Use XmlTextReader to load xml document from file : XmlTextReader : XML C# Examples


C# Examples » XML » XmlTextReader »

 

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



Output

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


HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo XML
» XmlTextReader