Gets the next node of this node in CSharp

Description

The following code shows how to gets the next node of this node.

Example


 /* w ww  .  j a va  2 s .c om*/
  
    
using System;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Linq;
using System.Collections;
using System.Collections.Generic;

public class MainClass
{
    public static void Main()
    {
        XElement xmlTree = new XElement("Root",
            new XElement("A1", 1),
            new XText("Some Text"),
            new XElement("A2",
                2,
                new XElement("GrandChild", "GrandChild Content")
            ),
            new XComment("a comment"),
            new XElement("A3")
        );
        XNode node = xmlTree.Element("A2");
        do
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(String.Format("NodeType: {0}", node.NodeType.ToString().PadRight(10)));
            switch (node.NodeType)
            {
                case XmlNodeType.Text:
                    sb.Append((node as XText).Value);
                    break;
                case XmlNodeType.Element:
                    sb.Append((node as XElement).Name);
                    break;
                case XmlNodeType.Comment:
                    sb.Append((node as XComment).Value);
                    break;
            }
            Console.WriteLine(sb.ToString());
        }
        while ((node = node.NextNode) != null);
    }
}




















Home »
  C# Tutorial »
    XML »




Load Parse
Document
Element
Attribute
Namespace
Query
Save
Schema
Style