Navigating the Document Via XPath in .NET

An important method for navigating XML documents is via XPath. XPath is a non-XML based specification for addressing arbitrary locations in XML documents. The XPath syntax is described in detail in Chapter 11, “Locating Components in XML Documents with XPath.” A basic usage of XPath with .NET is provided in this section.

Using XPath via the .NET XML interfaces is a straightforward process. The XPath interfaces require an XML parser that has been instantiated against the desired document in order to do their work. The first step to doing an XPath addressing is to generate an XmlReader, much as we did when generating an XmlDocument.

XmlReader xmlReader = new XmlTextReader("example.xml"); 

Next, an XPathDocument ...

Get Special Edition Using XML, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.