Section 5.7. Improve XML Manipulation with XPathDocument


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" ?> <Books>     <book>         <BookName>Programming C#</BookName>         <Author>Jesse Liberty</Author>         <Publisher>OReilly Media</Publisher>     </book>     <book>         <BookName>Programming ASP.NET</BookName>         <Author>Jesse Liberty</Author>         <Author>Dan Hurwitz</Author>         <Publisher>OReilly Media</Publisher>


Note: XPathDocument offers a flexible, fast, and efficient mechanism for loading and binding XML documents.
    </book>     <book>         <BookName>Visual C# Notebook</BookName>         <Author>Jesse Liberty</Author>         <Publisher>OReilly Media</Publisher>     </book>     <book>         <BookName>Visual Basic 2005 Notebook</BookName>         <Author>Matthew MacDonald</Author>         <Publisher>OReilly Media</Publisher>     </book>      </Books>

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 program


The work, of course, is done in the event handler for the Find button, as shown in Example 5-5.

Example 5-5. Using XPathDocument and a navigator
#region Using directives      using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Windows.Forms; using System.Xml.XPath;      #endregion      namespace XPathDocumentTester {    partial class Form1 : Form    {       public Form1( )       {          InitializeComponent( );       }            private void btnFind_Click(object sender, EventArgs e)       {          int numAuthors = 0;          string authorName = string.Empty;               // get the file as an XPathDocument          XPathDocument document = new XPathDocument("..\\..\\BookList.xml");               // Get a navigator over the document          XPathNavigator navigator = document.CreateNavigator( );               navigator.MoveToFirstChild( );   // move to books          navigator.MoveToFirstChild( );   // move to book               do                        // look at each node          {             // move to entries under book             navigator.MoveToFirstChild( );                   do                     // look at each sub entry under book             {                // if you match the book name, you found the book we want                if (navigator.Value =  = txtBookName.Text)                {                   do               // moving through the name, author, publisher                   {                      // if you found the author tag, get all the authors                      if (navigator.Name =  = "Author")                      {                         if (++numAuthors > 1)   // make a list                         {                            authorName += ", ";                         }                         authorName += navigator.Value;                      }                   } while (navigator.MoveToNext( ));                   }               // end if we found the book                  } while (navigator.MoveToNext( ));   // go to the entry under book                  navigator.MoveToParent( );         // done with this book, go up a level               } while (navigator.MoveToNext( ));      // go to next book               if (numAuthors =  = 0)          {             authorName = "Not Found";          }          lblAuthor.Text = authorName;       }    } }

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 .xml file 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 entries


Your cursor is on this line of code:

navigator.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 book


Press F10 to cycle through the do loop. Finally, you'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 displayed


5.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.



Visual C# 2005(c) A Developer's Notebook
Visual C# 2005: A Developers Notebook
ISBN: 059600799X
EAN: 2147483647
Year: 2006
Pages: 95
Authors: Jesse Liberty

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net