
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
838
|
Chapter 15: XML
This outputs the following:
Mrs. White
Mrs. Peacock
Discussion
XPath is a very versatile language for performing queries on XML-based data. In order
to accomplish this goal, you first create an XML fragment that looks like this:
<?xml version='1.0'?>
<Clue>
<Participant type='Perpetrator'>Professor Plum</Participant>
<Participant type='Witness'>Colonel Mustard</Participant>
<Participant type='Witness'>Mrs. White</Participant>
<Participant type='Witness'>Mrs. Peacock</Participant>
<Participant type='Witness'>Mr. Green</Participant>
</Clue>;
You then load this fragment into a StringReader, then construct an XPathDocument to
allow you to create an
XPathNavigator. This lets you use XPath syntax to query the
XML document shown in the preceding listing. The
XPathDocument instance wraps
the
MemoryStream so you can use XPath to locate nodes (as well as perform XSLT
transforms directly), and the
XPathNavigator gets the set of nodes selected by the
XPath expression.
byte[] bytes = Encoding.UTF8.GetBytes(xmlFragment);
MemoryStream memStream = new MemoryStream(bytes);
// Instantiate an XPathDocument using the MemoryStream.
XPathDocument xpathDoc = new XPathDocument(memStream);
// Get the navigator.
XPathNavigator xpathNav = xpathDoc.CreateNavigator( );
Now you have to determine