XmlTextReader

In this chapter you will learn:

  1. How to use XmlTextReader to read xml file

Read xml file

using System;/*ja  v  a 2  s . c  om*/
using System.Xml;

class XmlTextReaderSample {
  [STAThread]
  static void Main(string[] args) {
    XmlTextReader xmlTextReader = new XmlTextReader("sample.xml");
    while (xmlTextReader.Read()) {
      if (xmlTextReader.NodeType == XmlNodeType.Element) {
        Console.Out.WriteLine((new String(' ', xmlTextReader.Depth * 3)) + 
                "Name: <" + xmlTextReader.Name +  ">; Depth: " + 
                xmlTextReader.Depth.ToString() + "; Attributes count: " + 
                xmlTextReader.AttributeCount.ToString() + ";");
      }
    }
  }
}

Next chapter...

What you will learn in the next chapter:

  1. How to use XmlTextWriter to create xml file
  2. Save to a XmlTextWriter
Home » C# Tutorial » XML
Parse XML file
Parse XML String
Parse XML from URL
Element create
Attribute create
Comments create
XProcessingInstruction
XmlReader
Read double value from XML
XmlReader
XmlReaderSettings
XML formatter
XmlSerializer
XmlTextReader
XmlTextWriter
XmlWriter
XmlWriterSettings
Output XML to console