日期:2008-07-12  浏览次数:20434 次

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 te