Get sibling nodes after this node in CSharp

Description

The following code shows how to get sibling nodes after this node.

Example


using System;/*from  w  w w .  j  a v a 2s .  c om*/
using System.Linq;
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 XText("Text content."),
            new XElement("A1", "A1 content"),
            new XElement("A2", "A2 content"),
            new XText("More text content."),
            new XElement("A3", "A3 content")
        );
        XElement child = xmlTree.Element("A2");
        IEnumerable<XNode> nodes =
            from node in child.NodesAfterSelf()
            select node;
        foreach (XNode node in nodes)
        {
            Console.WriteLine("Node type: {0}  {1}",
                node.NodeType,
                node.NodeType == XmlNodeType.Text ? (node as XText).Value : "");
        }
    }
}




















Home »
  C# Tutorial »
    XML »




Load Parse
Document
Element
Attribute
Namespace
Query
Save
Schema
Style