Creating and Updating the XPathDocument2 Class



Creating and Updating the XPathDocument2 Class

As an example of how the XPathEditor makes it easy to update the XPathDocument2, we can load an XML document and make some changes. Throughout this chapter we will use the same XML document, which is a list of books from a bookstore. This may be familiar; it is taken from the XML QuickStart samples in the .NET Framework SDK. The books.xml document is shown in Listing 6.1.

The books.xml File
<?xml version="1.0" encoding="utf-8"?>
<!-- This file represents a fragment of a bookstore database -->
<bookstore>
  <book genre="autobiography" publicationdate="1981"
        ISBN="1-861-11-0">
    <title>The Autobiography of Benjamin Franklin</title>
    <author>
      <first-name>Benjamin</first-name>
      <last-name>Franklin</last-name>
    </author>
    <price>8.99</price>
  </book>
  <book genre="novel" publicationdate="1967" ISBN="0-201-63361-2">
    <title>The Confidence Man</title>
    <author>
      <first-name>Herman</first-name>
      <last-name>Melville</last-name>
    </author>
    <price>11.99</price>
  </book>
  <book genre="philosophy" publicationdate="1991"
        ISBN="1-861001-57-6">
    <title>The Gorgias</title>
    <author>
      <name>Plato</name>
    </author>
    <price>9.99</price>
  </book>
</bookstore>

Figure shows the books.xml document displayed in Internet Explorer.

1. The bookstore XML document viewed within Internet Explorer

graphics/06fig01.gif

The code in Listing 6.2 creates and loads an XPathDocument2 with the books.xml file, creates an XPathEditor, navigates to the <bookstore> element, and inserts a new book as a child element. It uses an XmlWriter to build the node tree for this and then saves the XML back to the same file.

Inserting a New Element with an XPathEditor
Dim doc As New XPathDocument2()
doc.Load("books.xml")

Dim xmledit As XPathEditor = doc.CreateXPathEditor()

xmledit.MoveToFirstChild()
xmledit.MoveToNextSibling()

Dim writer As XmlWriter = xmledit.CreateFirstChild()

writer.WriteStartElement("book")
writer.WriteAttributeString("genre", "technology")
writer.WriteAttributeString("publicationdate", "10-27-2003")
writer.WriteAttributeString("ISBN", "1-861003-11-1")
writer.WriteElementString("title", "ADO.NET and System.Xml V2")
writer.WriteStartElement("author")
writer.WriteElementString("first-name", "Alex")
writer.WriteElementString("last-name", "Homer")
writer.WriteEndElement()
writer.WriteStartElement("author")
writer.WriteElementString("first-name", "Mark")
writer.WriteElementString("last-name", "Fussell")
writer.WriteEndElement()
writer.WriteElementString("price", "29.99")
writer.WriteEndElement()

writer.Close()
doc.Save("books.xml")

Notice how a document can now be built using the XmlWriter class, meaning that this approach is identical whether you are writing to a file stream using the XmlTextWriter or writing to an XPathDocument2 (which is an in-memory node tree). Figure shows the new book titled ADO.NET and System.Xml V2 that has been added to the bookstore.

2. Inserting a new book with the XmlWriter

graphics/06fig02.gif