Tasks

Load XML into the document object model

Start with an XML document like this one that has a few books in a collection. It contains the basic things that you'd find in any XML document, including a namespace, elements that represent data, and attributes that describe the data.

Create an XmlReader object by using your schema, and then load that object into the DOM. Create an event handler that executes when code attempts to modify your XML file in ways that violate the rules of the schema.

Navigate the document tree

You can use properties to navigate around an XML document. But before you use any of them, let's quickly review a few terms. Your document is composed of nodes. Each node has as single parent node directly above it. The only node that does not have a parent node is the document root, as it is the top-level node. Most nodes can have child nodes, which are nodes directly below them. Nodes that are at the same level are siblings.

The following examples show you how to obtain the root node, jump to the first child node of the root node, access any of its child nodes, get back out to the parent node, and then navigate across sibling nodes.

Start with the root node

This example gets the root node and then uses that node to output the contents of the document to the console.

Imports System.IO
Imports System.Xml
public class Sample
public shared sub Main()
'Create the XmlDocument.
Dim doc as XmlDocument = new XmlDocument()
doc.Load("booksort.xml")
Dim book as XmlNode
Dim nodeList as XmlNodeList
Dim root as XmlNode = doc.DocumentElement
nodeList=root.SelectNodes("descendant::book[author/last-name='Austen']")
'Change the price on the books.
for each book in nodeList
book.LastChild.InnerText="15.95"
next
Console.WriteLine("Display the modified XML document....")
doc.Save(Console.Out)
end sub
end class

You can also get a collection of nodes by using the name of the node. For example, this example gets a collection of all book titles.

Option Explicit
Option Strict
Imports System.IO
Imports System.Xml
Public Class Sample
Public Shared Sub Main()
'Create the XmlDocument.
Dim doc As New XmlDocument()
doc.Load("books.xml")
'Display all the book titles.
Dim elemList As XmlNodeList = doc.GetElementsByTagName("title")
Dim i As Integer
For i = 0 To elemList.Count - 1
Console.WriteLine(elemList(i).InnerXml)
Next i
End Sub
End Class

Public Sub editBook(ByVal title As String, ByVal ISBN As String,
ByVal publicationDate As String, ByVal genre As String,
ByVal price As String, ByVal book As XmlNode, ByVal validateNode As Boolean,
ByVal generateSchema As Boolean)
Dim bookElement As XmlElement = CType(book, XmlElement)
' Get the attributes of a book.
bookElement.SetAttribute("ISBN", ISBN)
bookElement.SetAttribute("genre", genre)
bookElement.SetAttribute("publicationdate", publicationDate)
' Get the values of child elements of a book.
bookElement("title").InnerText = title
bookElement("price").InnerText = price
If validateNode Then
validateXML(generateSchema, bookElement.OwnerDocument)
End If
End Sub

Public Sub deleteBook(ByVal book As XmlNode)
Dim prevNode As XmlNode = book.PreviousSibling
book.OwnerDocument.DocumentElement.RemoveChild(book)
If ((prevNode.NodeType = XmlNodeType.Whitespace) _
OrElse (prevNode.NodeType = XmlNodeType.SignificantWhitespace)) Then
prevNode.OwnerDocument.DocumentElement.RemoveChild(prevNode)
End If
End Sub

Position nodes

This example shows two helper methods. One of them moves a node higher in a list. The other one moves a node lower.

These methods could be used in an application that enables users to move books up and down in a list of books. When a user chooses a book and presses an up or down button, your code could call methods like these to position the corresponding book node before or after other book nodes.

Puts all XmlText nodes in the full depth of the sub-tree underneath this XmlNode into a "normal" form where only markup (that is, tags, comments, processing instructions, CDATA sections, and entity references) separates XmlText nodes, that is, there are no adjacent XmlText nodes.