Mega Code Archive

 
Categories / C# Tutorial / XML
 

Use XmlTextReader to load xml document from file

using System; using System.Xml; class MainClass {   public static void Main()    {     XmlTextReader xtr = new XmlTextReader(@"c:\test.xml");     xtr.WhitespaceHandling = WhitespaceHandling.None;     XmlDocument xd = new XmlDocument();     xd.Load(xtr);     XmlNode xnodDE = xd.DocumentElement;     ChildDisplay(xnodDE, 0);     xtr.Close();   }   private static void ChildDisplay(XmlNode xnod, int level)   {     XmlNode xnodWorking;     String pad = new String(' ', level * 2);     Console.WriteLine(pad + xnod.Name + "(" + xnod.NodeType.ToString() + ": " + xnod.Value + ")");          if (xnod.NodeType == XmlNodeType.Element)     {       XmlNamedNodeMap mapAttributes = xnod.Attributes;       for(int i=0; i<mapAttributes.Count; i++)       {         Console.WriteLine(pad + " " + mapAttributes.Item(i).Name + " = " +  mapAttributes.Item(i).Value);       }     }          if (xnod.HasChildNodes)     {       xnodWorking = xnod.FirstChild;       while (xnodWorking != null)       {         ChildDisplay(xnodWorking, level+1);         xnodWorking = xnodWorking.NextSibling;       }     }   } } MyTestElements(Element: ) TestBoolean(Element: ) #text(Text: true)