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 ❘ 917<br />

to this:<br />

//create the declaration section<br />

XmlDeclaration newDec = doc.CreateXmlDeclaration("1.0",null,null);<br />

doc.AppendChild(newDec);<br />

//create the new root element<br />

XmlElement newRoot = doc.CreateElement("newBookstore");<br />

doc.AppendChild(newRoot);<br />

First, you create a new XmlDeclaration. The parameters are the version (always 1.0 for now), the<br />

encoding, <strong>and</strong> the st<strong>and</strong>alone flag. The encoding parameter should be set to a string that is part of<br />

the System.Text.Encoding class if null is not used (null defaults to UTF-8). The st<strong>and</strong>alone flag<br />

can be either yes, no, or null. If it is null, the attribute is not used <strong>and</strong> will not be included in the<br />

document.<br />

The next element that is created will become the DocumentElement. In this case, it is called newBookstore<br />

so that you can see the difference. The rest of the code is the same as in the previous example <strong>and</strong> works in<br />

the same way. This is booksEdit.xml, which is generated from the following code:<br />

<br />

<br />

<br />

The Case of the Missing Cookie<br />

<br />

C. Monster<br />

<br />

9.95<br />

<br />

<br />

You will want to use the XmlDocument class when you want to have r<strong>and</strong>om access to the document, or the<br />

XmlReader-based classes when you want a streaming-type model instead. Remember that there is a cost<br />

for the flexibility of the XmlNode-based XmlDocument class — memory requirements are higher <strong>and</strong> the<br />

performance of reading the document is not as good as when using XmlReader. There is another way to<br />

traverse an XML document: the XPathNavigator.<br />

using XPaThnaVigaTors<br />

An XPathNavigator is used to select, iterate through, <strong>and</strong> sometimes edit data from an XML<br />

document. An XPathNavigator can be created from an XmlDocument to allow editing capabilities or from<br />

an XPathDocument for read-only use. Because the XPathDocument is read-only, it performs very well.<br />

Unlike the XmlReader, the XPathNavigator is not a streaming model, so the same document can be used<br />

without having to re-read <strong>and</strong> parse.<br />

The XPathNavigaor is part of the System.Xml.XPath namespace. XPath is a query language used to select<br />

specific nodes or elements from an XML document for processing.<br />

The system.Xml.XPath namespace<br />

The System.Xml.XPath namespace is built for speed. It provides a read-only view of your XML<br />

documents, so there are no editing capabilities. Classes in this namespace are built to do fast iteration <strong>and</strong><br />

selections on the XML document in a cursory fashion.<br />

The following table lists the key classes in System.Xml.XPath <strong>and</strong> gives a short description of the purpose<br />

of each class.<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!