| « LINQ-to-XML Examples... | Convert DataTable to XML » |
XML and XPath expression examples
Given the following XML schema...

Here are a few XPath expressions and the nodes selected.
Select all child nodes…
/familytree/parent/child

Select child node element with name (attribute) of Sam
/familytree/parent/child[@name="Sam"]

Select distinct grandchild nodes…
/familytree/parent/child/grandchild[not(@name=preceding::grandchild/@name)]

In .NET, you can use either the XmlDocument or XPathNavigator objects to execute XPath expression in .NET framework code.
XmlDocument code samples…

For improved performance use XPathDocument...

XPath Expressions are a bit cryptic and only get worse as the complexity of your expression increases. For that reason, I highly recommend using the new LINQ to XML features in .NET.
LINQ queries are easier to read than complicated XPath expressions and perform just as well or better than using XmlDocument and XPathDocument objects.
Feedback awaiting moderation
This post has 21 feedbacks awaiting moderation...