Read XML data from xml file: Node type, name : Node Type « XML « C# / C Sharp






Read XML data from xml file: Node type, name

 
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>
*/

           
         
  








Related examples in the same category

1.XmlNodeType Text
2.XML reader: deal with various NodeTypeXML reader: deal with various NodeType
3.XML write: element, attribute, cddata, namespace and entity reference
4.Get boolean value from Xml
5.Get int value from Xml