13.07.2015 Views

Applied XML Programming for Microsoft .NET.pdf - Csbdu.in

Applied XML Programming for Microsoft .NET.pdf - Csbdu.in

Applied XML Programming for Microsoft .NET.pdf - Csbdu.in

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

How XPo<strong>in</strong>ter Uses XPathAn XPo<strong>in</strong>ter fragment identifier can be the name of a particular portion of the targetdocument, but it could also be a more complex and expressive XPath query. Forexample, you could l<strong>in</strong>k a piece of <strong>in</strong><strong>for</strong>mation us<strong>in</strong>g the follow<strong>in</strong>g syntax:<strong>in</strong>voices.xml#xpo<strong>in</strong>ter(/descendant::<strong>in</strong>voice[@id=201])This expression references the particular descendant node named hav<strong>in</strong>g anid attribute equal to 201.XPath <strong>in</strong> the <strong>XML</strong> DOMIn the .<strong>NET</strong> Framework, you can make use of XPath expressions <strong>in</strong> two ways: throughthe <strong>XML</strong> DOM or by means of a new and more flexible API based on the concept of theXPath navigator.In the <strong>for</strong>mer case, you use XPath expressions to select nodes with<strong>in</strong> the context of aliv<strong>in</strong>g <strong>in</strong>stance of the XmlDocument class. As we saw <strong>in</strong> Chapter 5, the XmlDocumentclass is the .<strong>NET</strong> Framework class that renders a given <strong>XML</strong> document as ahierarchical object model (<strong>XML</strong> DOM). This approach keeps the API close to the oldMS<strong>XML</strong> programm<strong>in</strong>g style and has probably been supplied mostly <strong>for</strong> compatibilityreasons.The alternative approach consists of creat<strong>in</strong>g an <strong>in</strong>stance of the XPathDocument classand obta<strong>in</strong><strong>in</strong>g from it an XPath navigator object. The navigator object is a generic XPathprocessor that works on top of any <strong>XML</strong> data store that exposes the IXPathNavigable<strong>in</strong>terface. Rendered through the XPathNavigator class, the XPath navigator objectparses and executes expressions us<strong>in</strong>g its Select method. XPath expressions can bepassed as pla<strong>in</strong> text or as preprocessed, compiled expressions. As you can see,although the classes <strong>in</strong>volved are different, the overall programm<strong>in</strong>g style is not muchdifferent from those pushed by MS<strong>XML</strong> and the .<strong>NET</strong> Framework <strong>XML</strong> DOM classes.This said, though, the XPath navigator object represents a quantum leap from theSelectNodes method of the XmlDocument class. For one th<strong>in</strong>g, it works on top of highlyspecialized document classes that implement IXPathNavigable and are optimized toper<strong>for</strong>m both XPath queries and XSL trans<strong>for</strong>mations. In contrast, the XmlDocumentclass is a generic data conta<strong>in</strong>er class that <strong>in</strong>corporates an XPath processor but is notbuilt around it.Several classes <strong>in</strong> the .<strong>NET</strong> Framework implement the IXPathNavigable <strong>in</strong>terface, thusmak<strong>in</strong>g their contents automatically selectable by XPath expressions. We'll look at thenavigation API <strong>in</strong> more detail <strong>in</strong> the section "The .<strong>NET</strong> XPath Navigation API," on page263. For now, let's review the XPath support built <strong>in</strong>to the XmlDocument class.The <strong>XML</strong> DOM Node Retrieval APIWhen us<strong>in</strong>g XPath queries to query an <strong>XML</strong> DOM <strong>in</strong>stance, you can use theSelectNodes method of the XmlDocument class. In particular, SelectNodes returns acollection that conta<strong>in</strong>s <strong>in</strong>stances of all the XmlNode objects that match the specifiedexpression. If you don't need the entire node-set, but <strong>in</strong>stead plan to use the query tolocate the root of a particular subtree, use the SelectS<strong>in</strong>gleNode method.SelectS<strong>in</strong>gleNode takes an XPath expression and returns a reference to the first matchfound.The SelectNodes and SelectS<strong>in</strong>gleNode methods per<strong>for</strong>m identical functionality to themethods available from the Component Object Model (COM)– based MS<strong>XML</strong> librarythat script and <strong>Microsoft</strong> W<strong>in</strong>32 applications normally use. It is worth not<strong>in</strong>g that these208

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

Saved successfully!

Ooh no, something went wrong!