Load xml document in XmlTextReader to XmlDocument : XmlTextReader « XML « C# / CSharp Tutorial






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)








30.7.XmlTextReader
30.7.1.Create XmlTextReader from StringReader
30.7.2.Read and write XML document
30.7.3.XmlTextWriter: Write Comment
30.7.4.XmlTextWriter: write start element
30.7.5.XmlTextWriter: write string
30.7.6.Copy one xml document as a sub element into another xml document
30.7.7.Use XmlTextReader to load xml document from file
30.7.8.Load xml document in XmlTextReader to XmlDocument
30.7.9.XmlTextReader: read and move
30.7.10.XmlTextReader: Skip