Load xml document in XmlTextReader to XmlDocument : XmlTextReader : XML C# Examples


C# Examples » XML » XmlTextReader »

 

Load xml document in XmlTextReader to XmlDocument









    
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;
            }
        }
    }
}
    
   
  
   



Output

MyTestElements(Element: )
  TestBoolean(Element: )
    #text(Text: true)


HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo XML
» XmlTextReader