5.7. Improve XML Manipulation with XPathDocument.NET 2.0 provides a new, much-improved XPathDocument class that offers a cursor-based XML reader that is more flexible than XmlWriter and XmlReader and is more efficient (and faster) than XmlDocument.With XPathDocument it is not necessary for the entire XmlDocument to be loaded into memory. What's more, XPathDocument supports data binding. You can display the XPathDocument in a control just by setting the DataSource property to point to the XPathDocument. 5.7.1. How do I do that?Create a new Windows program (called XPathDocumentTester). Add an XML document, named BookList.xml: <?xml version="1.0" encoding="utf-8" ?>Note: XPathDocument offers a flexible, fast, and efficient mechanism for loading and binding XML documents. </book>On the form, add a label and a text box, and then add a second label beneath the first one as well, as a button, as shown in Figure 5-28. Name the text box txtBookName, name the second label lblAuthor, and name the button btnFind. Figure 5-28. The XPathEditor Tester programbutton, as shown in Example 5-5. Example 5-5. Using XPathDocument and a navigator#region Using directives 5.7.2. What just happened?The best way to see how this code works is to step through it in the debugger. Place a break point on this line: XPathDocument document = new XPathDocument("..\\..\\BookList.xml");Because the default place to look for the .xmlfile is in the debug directory, you are setting the relative path up to where the source code is held.Run the debugger to this line. Enter the title of a book (e.g., Programming ASP.NET) and click Find. The debugger stops at your break point. Press F10 and hover your mouse cursor over the document. You'll find that you have an object of type XPathDocument.Press F10 to get the navigator. Open the Watch Window and add three entries, as shown in Figure 5-29. Figure 5-29. Watch Window entriesnavigator.MoveToFirstChild( );and navigator.Name is blank. Press F10 and the navigator.Name value changes to Books. You want to go down one more level. Press F10 and the navigator.Name value changes to Book. Aha! Now you are examining a Book node, which is just what you want.Enter the do loop and execute the MoveToFirstChild command. This sets the navigator.Name value to BookName and the navigator.Value value to Programming C#. This is not the book you are looking for, so the if statement fails, as shown in Figure 5-30. Figure 5-30. Not the correct bookyou've looked at all the fields for that book and you hit this line: navigator.MoveToParent( );This brings you back up to the book level. Continue to press F10 and you move to the next book and then enter the do loop again, where you move to the first child of that book. Now you are ready to examine the book name, and this time you have the right book. The if statement succeeds, so you enter the innermost do loop to iterate through the entries for this book.When the navigator.Name field is equal to Author you have the right author for the right book, so you can add to the authorName string (the final if statement just puts commas between names for multiple authors).When you exit all of this code, you've grabbed the names for the authors of the chosen book and you've displayed them in the label, as shown in Figure 5-31. Figure 5-31. Authors retrieved and displayed5.7.3. What about......selecting a portion of the XML document rather than searching the entire document? Can I do that?Because you know the structure of your document, you can use XPath to narrow the selection. This is shown in the next lab. 5.7.4. Where can I learn more?Learn more about XPathDocument on the MSDN site in the article titled "XPathDocumentClass." Also, a good weblog article is on DotNetJunkies at http://dotnetjunkies.com/WebLog/jcmag/archive/2004/02/09/6862.aspx. |