namespace testxmlhelper {
using system;
using system.xml;
using system.collections;
using robbirdwell.xmlhelper;
/*******
the following classes serve as a simple test app for the xmlhelper module.
it attempts to load "mytestobj.xml" – the text for this is at the bottom
of this listing.
this is a work in progress and provided solely to test and learn c#.
********/
class testxmlhelper // test class for xmlhelper
{
static int main(string[] args)
{
console.writeline("begin xml load test…");
mymanager nm = new mymanager();
if (nm.load() == true)
console.writeline("end load test – xml data loaded successfully!");
else
console.writeline("end load test – xml load failed!");
console.writeline("begin xml create test…");
// demonstrate how we can create an xml document from scratch…
xmlhelper myxml = new xmlhelper();
myxml.loadxml("<myrootnode/>", xmlhelper.loadtype.fromstring);
// now create some children – elements & attributes…
xmlelement xmlchildfolder = myxml.createnodeelement(myxml.rootnode, "childfolder", "");
xmlattribute xmlchildattribute1 = myxml.createnodeattribute(xmlchildfolder, "price", "12.00");
xmlelement xmlchildelement = myxml.createnodeelement(xmlchildfolder, "childelementofchildfolder", "test element with a <tag> to test encoding");
xmlelement xmlsubchildfolder = myxml.createnodeelement(xmlchildfolder, "subfolderofchildfolder", "");
xmlelement xmlsubchildelement = myxml.createnodeelement(xmlsubchildfolder, "subchildelement", "this is a sub element!");
xmlattribute xmlchildattribute2 = myxml.createnodeattribute(xmlsubchildfolder, "price", "10.00");
// now save this xml document…
myxml.savetofile("myxmltest.xml");
console.writeline("end xml create test – view myxmltest.xml");
// now demonstrate how we can load xml, modify it, and resave it!
console.writeline("begin xml load/modify/save test…");
xmlhelper modifyxml = new xmlhelper();
modifyxml.loadxml("myxmltest.xml", xmlhelper.loadtype.fromlocalfile);
// change something
arraylist foundnodes = modifyxml.getchildnodesfromcriteria("descendant::childfolder");
foreach (xmlnode thenode in foundnodes)
{
xmlnode nodetomodify = modifyxml.getfirstchildxmlnode(thenode, "childelementofchildfolder");
if (nodetomodify != null)
modifyxml.modifynodeelementvalue(nodetomodify, "modified value: " + datetime.now);
}
modifyxml.savetofile("mymodifiedxmltest.xml");
console.writeline("end xml load/modify/save test – see mymodifiedxmltest.xml to see modification results to childelementofchildfolder");
// now show that we can load xml from a url…
// note: if behind a firewall this fail – comment out if necessary!
console.writeline("start xml load/save test from url");
xmlhelper readfromurl = new xmlhelper();
string stesturl = "http://www.birdwellmusic.com/bmpdata.xml";
readfromurl.loadxml(stesturl, xmlhelper.loadtype.fromurl);
readfromurl.savetofile("fromurl.xml");
console.writeline("end xml load/save test from url – see fromurl.xml");
return 0;
}
}
// a test class that contains some data, arrays, etc. the usual bits…
class mytestobj
{
private string m_id;
private string m_datecreated;
private string m_datemodified;
private string m_text;
private arraylist m_urls = new arraylist();
public string id
{
get {return m_id; }
set {m_id = value;}
}
public string datecreated
{
get {return m_datecreated; }
set {m_datecreated = value; }
}
public string datemodified
{
get {return m_datemodified; }
set {m_datemodified = value; }
}
public string text
{
get {return m_text; }
set {m_text = value;}
}
public void addmyurl(myurl myurl)
{
m_urls.add(myurl);
}
}
// another test class – contains some members to store url link info…
class myurl
{
private string m_label;
private string m_link;
public string label
{
get {return m_label; }
set {m_label = value;}
}
public string link
{
get {return m_link; }
set {m_link = value;}
}
}
// a test "manager" class that wraps up our test classes and uses the xmlhelper
class mymanager
{
private xmlhelper m_xmlhelper;
private arraylist m_mytestobjarray = new arraylist();
public mymanager()
{
m_xmlhelper = new xmlhelper();
}
public void addmytestobj(mytestobj mytestobj)
{
m_mytestobjarray.add(mytestobj);
}
public bool load()
{
if (m_xmlhelper.loadxml("mytestobj.xml", xmlhelper.loadtype.fromlocalfile) == false)
return false;
xmlnodelist objnodes = m_xmlhelper.getchildnodesfromroot("obj");
foreach (xmlnode xmlnode in objnodes)
{
mytestobj mytestobj = new mytestobj();
mytestobj.id = m_xmlhelper.getattributevalue(xmlnode, "id");
mytestobj.datecreated = m_xmlhelper.getattributevalue(xmlnode, "datecreated");
mytestobj.datemodified = m_xmlhelper.getattributevalue(xmlnode, "datemodified");
mytestobj.text = m_xmlhelper.getchildelementvalue(xmlnode, "text");
console.writeline("debug: id={0}, datecreated={1}, datemodified={2}, text={3}", mytestobj.id, mytestobj.datecreated, mytestobj.datemodified, mytestobj.text);
arraylist urlnodes = m_xmlhelper.getrecursivechildnodesfromparent(xmlnode, "url");
foreach (xmlnode urlnode in urlnodes)
{
myurl myurl = new myurl();
myurl.label = m_xmlhelper.getchildelementvalue(urlnode, "label");
myurl.link = m_xmlhelper.getchildelementvalue(urlnode, "link");
mytestobj.addmyurl(myurl);
console.writeline("debug: url link={0}", myurl.link);
}
addmytestobj(mytestobj);
}
return true;
}
}
} // end testxmlhelper namespace
/*************
mytestobj.xml
<note>
<obj id="123" datecreated="february 12, 2001 12:50pm" datemodified="february 16, 2001 12:50pm">
<text>this is a test</text>
<urls>
<url>
<label>rob test link</label>
<link>http://www.birdwellmusic.com</link>
</url>
<url>
<label>search link</label>
<link>http://www.google.com</link>
</url>
</urls>
</obj>
<obj id="246" datecreated="march 8, 2001 12:50pm" datemodified="march 8, 2001 12:50pm">
<text>this is a second node!</text>
<urls>
<url>
<label>test 2</label>
<link>www.google.com</link>
</url>
<url>
<label>test 2</label>
<link>www.hp.com</link>
</url>
</urls>
</obj>
</note>
***********/
