AddThis Social Bookmark Button

Print

XML DataSource Controls in .NET 2.0
Pages: 1, 2

XPath

.NET 2.0 provides a new, much improved XPathDocument class that offers a cursor-based XML reader that is both more flexible than either XMLWriter or XMLReader and more efficient (and faster) than XMLDocument. With XPathDocument, it is not necessary for the entire XML document to be loaded into memory, and XPathDocument supports data binding. You can display the XPathDocument in a control just by setting the DataSource property to point to the XPathDocument.



This time create a Windows application named XPATH. Add an XML document, named BookList.xml, but note that this version of BookList.xml is slightly different from the previous version. The book element does not have an attribute for the BookName; instead, the book name is a sub-element of book.


<?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>
  </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>

Return to the Windows form and add a label (named label1) and text box (named txtBookName). Then add a second label (named lblAuthor), and a button (named btnFind), as shown in Figure 7:

Figure 7
Figure 7.

Implement the Find button's click method with the following code, which will be explained below.


protected 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();
  
  // move to books
  navigator.MoveToFirstChild();   
  navigator.MoveToFirstChild();   
    
  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)
      {
        // moving through the name, author, publisher
        do               
        {
           // 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

      // go to the entry under book
    } while (navigator.MoveToNext());   

    // done with this book, go up a level
    navigator.MoveToParent();         

  } 

// go to next book
  while (navigator.MoveToNext());      
  
    if (numAuthors == 0)
    {
      authorName = "Not Found";
    }
    lblAuthor.Text = authorName;
  }
}

Be sure to add this using statement at the top of your file:

using System.Xml.XPath;

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 to where the source code is held. Run the debugger. Enter the name of a book (e.g., "Programming ASP.NET") and click Find. The debugger stops at your break point. Hit F10 and hover over the document. You'll find that you have an object of the type XPathDocument. Hit F10 to get the navigator. Open the watch window and add three entries, as shown in Figure 8:

Figure 8
Figure 8.

Move your cursor so that it is on this line of code:

navigator.MoveToFirstChild();

and the Navigator.Name field is blank. Hit F10 and the Navigator.Name value changes to "Books". You want to go one more level down. Hit F10 and the Navigator.Name value changes to "book". Aha! You are now examining a book node, just what you want.

Enter the do loop and execute the MoveToFirstChild command. This sets the Navigator.Name value to "BookName" and the NavigatorValue to "Programming C#"--not the book you are looking for, so the if statement fails, as shown in Figure 9:

Figure 9
Figure 9.

Hit 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 hit 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. You are now 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, and 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 it in the label, as shown in Figure 10:

Figure 10
Figure 10.

Jesse Liberty is a senior program manager for Microsoft Silverlight where he is responsible for the creation of tutorials, videos and other content to facilitate the learning and use of Silverlight. Jesse is well known in the industry in part because of his many bestselling books, including O'Reilly Media's Programming .NET 3.5, Programming C# 3.0, Learning ASP.NET with AJAX and the soon to be published Programming Silverlight.

.NET & XML

Related Reading

.NET & XML
By Niel Bornstein

Read more Liberty on Whidbey columns.

Return to ONDotnet.com