Read an XML file and displays each of the nodes. : Xml Node « XML « C# / CSharp Tutorial






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

public class Sample {
  public static void Main() {
     XmlTextReader reader = null;
     try {
        reader = new XmlTextReader("items.xml");
        reader.WhitespaceHandling = WhitespaceHandling.None;

        while (reader.Read()) {
           switch (reader.NodeType) {
             case XmlNodeType.Element:
               Console.Write("<{0}>", reader.Name);
               break;
             case XmlNodeType.Text:
               Console.Write(reader.Value);
               break;
             case XmlNodeType.CDATA:
               Console.Write("CDATA:", reader.Value);
               break;
             case XmlNodeType.ProcessingInstruction:
               Console.Write("<?{0} {1}?>", reader.Name, reader.Value);
               break;
             case XmlNodeType.Comment:
               Console.Write("<!--{0}-->", reader.Value);
               break;
             case XmlNodeType.XmlDeclaration:
               Console.Write("<?xml version='1.0'?>");
               break;
             case XmlNodeType.Document:
               break;
             case XmlNodeType.DocumentType:
               Console.Write("<!DOCTYPE {0} [{1}]", reader.Name, reader.Value);
               break;
             case XmlNodeType.EntityReference:
               Console.Write(reader.Name);
               break;
             case XmlNodeType.EndElement:
               Console.Write("</{0}>", reader.Name);
               break;
           }       
        }           
     }finally {
        if (reader!=null)
          reader.Close();
     }
  }
}








30.11.Xml Node
30.11.1.Read an XML file and displays each of the nodes.
30.11.2.Find nodes by name
30.11.3.Select Nodes By Namespace from XmlDocument
30.11.4.XmlNode: InsertAfter FirstChild
30.11.5.Xml Node List
30.11.6.Select nodes from XmlDocument
30.11.7.Recursively display XmlNode
30.11.8.Replace Children
30.11.9.Root Node
30.11.10.Insert after
30.11.11.Append element
30.11.12.Get current value and depth during the XML document reading process
30.11.13.Read XML document by node type
30.11.14.Check for Xml node name
30.11.15.Extract elements for types
30.11.16.Get elements
30.11.17.Select By Specific Author Node
30.11.18.Write types to the console
30.11.19.Select node by node text