15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Using XPathnavigators ❘ 919<br />

If the XPathNavigator was created from an XPathDocument, it is read-only. If it is created from an<br />

XmlDocument, the XPathNavigator can be used to edit the document. This can be verified by checking the<br />

CanEdit property. If it is true, you can use one of the Insert methods. InsertBefore <strong>and</strong> InsertAfter<br />

will create a new node either before or after the current node. The source of the new node can be from an<br />

XmlReader or a string. Optionally, an XmlWriter can be returned <strong>and</strong> used to write the new node information.<br />

Strongly typed values can be read from the nodes by using the ValueAs properties. Notice that this is<br />

different from XmlReader, which used ReadValue methods.<br />

XPathnodeiterator<br />

XPathNodeIterator can be thought of as the equivalent of a NodeList or a NodeSet in XPath. This object<br />

has two properties <strong>and</strong> three methods:<br />

➤<br />

➤<br />

➤<br />

➤<br />

➤<br />

Clone() — Creates a new copy of itself<br />

Count — Number of nodes in the XPathNodeIterator object<br />

Current — Returns an XPathNavigator pointing to the current node<br />

CurrentPosition() — Returns an integer with the current position<br />

MoveNext() — Moves to the next node that matches the XPath expression that created the<br />

XPathNodeIterator<br />

The XPathNodeIterator is returned by the XPathNavigator Select methods. You use it to iterate<br />

over the set of nodes returned by a Select method of the XPathNavigator. Using the MoveNext method<br />

of the XPathNodeIterator does not change the location of the XPathNavigator that created it.<br />

Using Classes from the XPath namespace<br />

The best way to see how these classes are used is to look at some code that iterates through the books.xml<br />

document. This will allow you to see how the navigation works. In order to use the examples, you first add<br />

a reference to the System.Xml.Xsl <strong>and</strong> System.Xml.XPath namespaces:<br />

using System.Xml.XPath;<br />

using System.Xml.Xsl;<br />

For this example, you use the file booksxpath.xml. It is similar to the books.xml file that you have been<br />

using, except that there are a couple of extra books added. Here’s the form code, which is part of the<br />

XmlSample project:<br />

private void button1_Click(object sender, EventArgs e)<br />

{<br />

//modify to match your path structure<br />

XPathDocument doc = new XPathDocument("books.xml");<br />

//create the XPath navigator<br />

XPathNavigator nav = ((IXPathNavigable)doc).CreateNavigator();<br />

//create the XPathNodeIterator of book nodes<br />

// that have genre attribute value of novel<br />

XPathNodeIterator iter = nav.Select("/bookstore/book[@genre='novel']");<br />

textBox1.Text = "";<br />

while (iter.MoveNext())<br />

{<br />

XPathNodeIterator newIter =<br />

iter.Current.SelectDescendants(XPathNodeType.Element, false);<br />

while (newIter.MoveNext())<br />

{<br />

textBox1.Text += newIter.Current.Name + ": " +<br />

newIter.Current.Value + "\r\n";<br />

}<br />

}<br />

}<br />

code snippet frmNavigator.cs<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!