TextXmlHelp
发表于:2007-06-30来源:作者:点击数:
标签:
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
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 Su
clearcase/" target="_blank" >ccessfully!");
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("descend
ant::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 = "";
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>
***********/
原文转自:http://www.ltesting.net