Gets the previous sibling node of this node.
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.PreviousNode) != null);
}
}
Related examples in the same category