XML C# Book

The examples in this section all use the following XML file:




Jack
Smith


Tom
James



The SelectXXX methods accept an XPath query string.
For example, the following finds the firstname node of an XmlDocument:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
using System.Text;
using System.IO;
class Program
{
static void Main()
{
XmlDocument doc = new XmlDocument();
doc.Load("customers.xml");
XmlNode n = doc.SelectSingleNode("customers/customer[firstname='Jack']");
Console.WriteLine(n.InnerText);
}
}
The output:
JackSmith
Common XPath operators
Operator Description
/ Children
// Recursively children
. Current node (usually implied)
.. Parent node
* Wildcard
@ Attribute
[] Filter
: Namespace separator