CSharp · Console
using System;
using System.Xml.XPath;
using System.Xml;
public class Program
{
public static void Main()
{
const string xml = @"<?xml version=""1.0"" encoding=""utf-8""?>
<!-- chocolate.xml -->
<report month=""8"" year=""2006"">
<title>Chocolate bar sales</title>
<brand>
<name>Lindt</name>
<units>27408</units>
</brand>
<brand>
<name>Callebaut</name>
<units>8203</units>
</brand>
<brand>
<name>Valrhona</name>
<units>22101</units>
</brand>
<brand>
<name>Perugina</name>
<units>14336</units>
</brand>
<brand>
<name>Ghirardelli</name>
<units>19268</units>
</brand>
</report>";
var doc = new XmlDocument();
doc.LoadXml(xml);
var navigator = doc.CreateNavigator();
var nodes = navigator.Select("/report/brand/name");
Console.WriteLine("Count: " + nodes.Count);
// Go to first node
nodes.MoveNext();
Console.WriteLine("Value of first node: " + nodes.Current.Value);
Console.WriteLine("Index of first node: " + nodes.CurrentPosition);
}
}