O'Reilly logo

Visual C# 2005: A Developer's Notebook by Jesse Liberty

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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.

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required