Read XML data from xml file : Node Type : XML C# Source Code


Custom Search

C# Source Code » XML » Node Type »

 

Read XML data from xml file








    

using System;
using System.IO;
using System.Xml;

  class TestXMLReader
  {
    static void Main(string[] args)
    {
      TestXMLReader tstObj = new TestXMLReader();

      StreamReader myStream = new StreamReader("books.xml");
      XmlTextReader xmlTxtRdr = new XmlTextReader(myStream);
      while(xmlTxtRdr.Read())
      {
        if(xmlTxtRdr.NodeType == XmlNodeType.Element 
          && xmlTxtRdr.Name == "A")
        {
          tstObj.ProcessMyDocument(xmlTxtRdr);
        }
      }

    }
    public void ProcessMyDocument(XmlTextReader reader)
    {
      Console.WriteLine("Start processing:" + reader.GetAttribute("property"));
      while(!(reader.NodeType == XmlNodeType.EndElement && reader.Name == "B")
        && reader.Read()) {
        if(reader.NodeType == XmlNodeType.Element && reader.Name == "C") {
          Console.WriteLine("itemcode:" + reader.GetAttribute("c"));
        }
      }
    }
  }

/*
<books>
  <A property="a">
    <B>text</B>
    <C c="aaa" >textg</C>
    <D>99999</D>
  </A>
</books>
*/

           
       
    
   
  
   







HTML code for linking to this page:

Follow Navioo On Twitter

C# Source Code

 Navioo XML
» Node Type