XmlNodeType

NodeType is of type XmlNodeType, which is an enum with these members:

Consider the following XML file:

 

    <?xml version="1.0" encoding="utf-8" standalone="yes"?>
    <customer id="123" status="archived">
        <firstname>Jack</firstname>
        <lastname>Smith</lastname>
    </customer>

  

Two string properties on XmlReader provide access to a node's content: Name and Value. Depending on the node type, either Name or Value (or both) is populated:


using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
using System.Text;
using System.IO;
class Program
{
    static void Main()
    {
        XmlReaderSettings settings = new XmlReaderSettings();
        settings.IgnoreWhitespace = true;
      

        using (XmlReader r = XmlReader.Create("customer.xml", settings))
            while (r.Read())
            {
                Console.Write(r.NodeType.ToString().PadRight(17, '-')); Console.Write("> ".PadRight(r.Depth * 3));

                switch (r.NodeType)
                {
                    case XmlNodeType.Element:
                    case XmlNodeType.EndElement: Console.WriteLine(r.Name); break;

                    case XmlNodeType.Text:
                    case XmlNodeType.CDATA:
                    case XmlNodeType.Comment:
                    case XmlNodeType.XmlDeclaration: Console.WriteLine(r.Value); break;

                    case XmlNodeType.DocumentType:
                        Console.WriteLine(r.Name + " - " + r.Value); break;

                    default: break;
                }
            }
    }
}

The output:


XmlDeclaration---> version="1.0" encoding="utf-8" standalone="yes"
Element----------> customer
Element---------->  firstname
Text------------->     Jack
EndElement------->  firstname
Element---------->  lastname
Text------------->     Smith
EndElement------->  lastname
EndElement-------> customer
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.