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

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

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

methods are not part of the official W3C <strong>XML</strong> DOM specification but represent, <strong>in</strong>stead,<strong>Microsoft</strong> extensions to the standard <strong>XML</strong> DOM.At the application level, <strong>XML</strong> DOM methods and the XPath navigator supply differentprogramm<strong>in</strong>g <strong>in</strong>terfaces, but <strong>in</strong>ternally they run absolutely equivalent code.The SelectNodes Internal ImplementationThe SelectNodes method <strong>in</strong>ternally employs a navigator object to retrieve the list ofmatch<strong>in</strong>g nodes. The return value of the navigator's Select method is then used to<strong>in</strong>itialize an undocumented <strong>in</strong>ternal node list class named System.Xml.XPath.XPathNodeList. As you have probably guessed, this class <strong>in</strong>herits from XmlNodeList,which is a documented class. To verify this statement, compile and run the follow<strong>in</strong>gsimple code:XmlDocument doc = new XmlDocument();doc.Load(fileName);XmlNodeList nodes = doc.SelectNodes("child::*");Console.WriteL<strong>in</strong>e(nodes.ToStr<strong>in</strong>g());The true type of the variable nodes is XPathNodeList. If you try to reference that type <strong>in</strong>your code, you get a compile error due to the protection level of the class.What's the difference between us<strong>in</strong>g SelectNodes and the XPath navigator object? TheSelectNodes method uses a navigator that works on top of a generic <strong>XML</strong> documentclass—the XmlDocument class. The SelectNodes method's navigator object is, <strong>in</strong> fact,created by the XmlDocument class's CreateNavigator method. If you choose to publiclymanage a navigator, you normally create it from a more specific and XPath-optimizeddocument class—the XPathDocument class.The XPath expression is passed to the navigator as pla<strong>in</strong> text:XmlNodeList SelectNodes(str<strong>in</strong>g xpathExpr, XmlNamespaceManagernsm)Interest<strong>in</strong>gly enough, however, if you use this overload of the SelectNodes method thathandles namespace <strong>in</strong><strong>for</strong>mation, the XPath expression is first compiled and thenpassed to the processor.As we'll see <strong>in</strong> the section "Compil<strong>in</strong>g Expressions," on page 274, only compiled XPathexpressions support namespace <strong>in</strong><strong>for</strong>mation. In particular, they get namespace<strong>in</strong><strong>for</strong>mation through an <strong>in</strong>stance of the XmlNamespaceManager class.The SelectS<strong>in</strong>gleNode Internal ImplementationThe SelectS<strong>in</strong>gleNode method is really a special case of SelectNodes. Un<strong>for</strong>tunately,there is no per<strong>for</strong>mance advantage <strong>in</strong> us<strong>in</strong>g SelectS<strong>in</strong>gleNode <strong>in</strong> lieu of SelectNodes.The follow<strong>in</strong>g pseudocode illustrates the current implementation of theSelectS<strong>in</strong>gleNode method:public XmlNode SelectS<strong>in</strong>gleNode(str<strong>in</strong>g xpathExpr){XmlNodeList nodes = SelectNodes(xpathExpr);return nodes[0];}The SelectS<strong>in</strong>gleNode method <strong>in</strong>ternally calls SelectNodes and retrieves all the nodesthat match a given XPath expression. Next it simply returns the first selected node to209

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

Saved successfully!

Ooh no, something went wrong!