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.

While the secondary thread works, the user can freely use the application's user<strong>in</strong>terface and the huge size of the <strong>XML</strong> file is no longer a serious issue—at least as itperta<strong>in</strong>s to load<strong>in</strong>g.Extract<strong>in</strong>g <strong>XML</strong> DOM SubtreesYou normally build the <strong>XML</strong> DOM by load<strong>in</strong>g the entire <strong>XML</strong> document <strong>in</strong>to memory.However, the XmlDocument class also provides the means to extract only a portion ofthe document and return it as an <strong>XML</strong> DOM subtree. The key method to achieve thisresult is ReadNode, shown here:public virtual XmlNode ReadNode(XmlReader reader);The ReadNode method beg<strong>in</strong>s to read from the current position of the given reader anddoesn't stop until the end tag of the current node is reached. The reader is then leftimmediately after the end tag. For the method to work, the reader must be positionedon an element or an attribute node.ReadNode returns an XmlNode object that conta<strong>in</strong>s the subtree represent<strong>in</strong>geveryth<strong>in</strong>g that has been read, <strong>in</strong>clud<strong>in</strong>g attributes. ReadNode is different fromChildNodes <strong>in</strong> that it recursively processes children at any level and does not stop atthe first level of sibl<strong>in</strong>gs.Visit<strong>in</strong>g an <strong>XML</strong> DOM SubtreeSo far, we've exam<strong>in</strong>ed ways to get <strong>XML</strong> DOM objects out of an <strong>XML</strong> reader. Is itpossible to call an <strong>XML</strong> reader to work on an <strong>XML</strong> DOM document and have the readervisit the whole subtree, one node after the next?Chapter 2 <strong>in</strong>troduced the XmlNodeReader class, with the promise to return to it later.Let's do that now. The XmlNodeReader class is an <strong>XML</strong> reader that enables you toread nodes out of a given <strong>XML</strong> DOM subtree.Just as XmlTextReader visits all the nodes of the specified <strong>XML</strong> file, XmlNodeReadervisits all the nodes that <strong>for</strong>m an <strong>XML</strong> DOM subtree. Note that the node reader is reallycapable of travers<strong>in</strong>g all the nodes <strong>in</strong> the subtree no matter the level of depth. Let'sreview a situation <strong>in</strong> which you might want to take advantage of XmlNodeReader.The XmlNodeReader ClassSuppose you have selected a node about which you need more <strong>in</strong><strong>for</strong>mation. To scan allthe nodes that <strong>for</strong>m the subtree us<strong>in</strong>g <strong>XML</strong> DOM, your only option is to use a recursivealgorithm like the one discussed with the LoopThroughChildren method <strong>in</strong> the section"Load<strong>in</strong>g <strong>XML</strong> Documents," on page 219. The XmlNodeReader class gives you aneffective, and ready-to-use, alternative, shown here:// Select the root of the subtree to processXmlNode n = root.SelectS<strong>in</strong>gleNode("Employee[@id=2]");if (n != null){// Instantiate a node reader objectXmlNodeReader nodeReader = new XmlNodeReader(n);// Visit the subtreewhile (nodeReader.Read()){// Do someth<strong>in</strong>g with the node...186

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

Saved successfully!

Ooh no, something went wrong!