Using XML in Visual Basic .NET

XML is not new to Visual Basic programmers; it has been available through a variety of methods in Visual Basic 6.0 and earlier, but it is quite a bit more prominent within the .NET Framework. Although you might already have some experience with XML, this section starts with a brief explanation of the format before getting into the .NET methods for working with it.

What Is XML?

Extensible Markup Language (XML) is a way of adding information to text. XML is the current popular term in software—entire companies and product lines have been created or modified just to add XML to the product brochures—and there are many myths and misconceptions about what it can do and is intended to do.

XML is similar to HTML; in fact, they are related. One (overly simplified) way of thinking about XML is that XML is to data (and databases) what HTML is to text. Another way of thinking about XML is that it is like HTML, but you can define your own tags. There are some differences, however. XML is much stricter about what you put into it. Table 10.2 describes some of the more important components of XML files, whereas Table 10.3 describes a few of the rules that apply to an XML file. (Refer to the sample XML file in Listing 10.2 as you go along.)

Table 10.2. XML File Components
ITEMDISCUSSION
XML declarationXML files should start with a line similar to the first line of the sample XML file. This is the XML declaration that identifies the version, and optionally the formatting of the XML document.
ElementElements are the most important part of an XML document. They are composed of a start tag and an end tag. There may be text, or other elements between the two. There are many elements in the sample XML file; for example, book, title, authors, and author are all elements, as are chapters and chapter.
AttributesAn attribute is some characteristic of an Element. It is written within an Element's start tag. All attributes must be surrounded in quotes, even if they are numbers. In the sample document, ISBN, count, Id, and name are all attributes.
CommentsXML files can include comments. The way you mark a comment is just as with HTML files, you wrap the comment in the characters: <!— and —>.

Table 10.3. Rules for XML Documents
RULEDISCUSSION
There can only be one root elementThere must be one, and only one root element for an XML document. For example this is not valid XML:
<author>Duncan Mackenzie
  <title>Visual Basic .NET KickStart</title>
</author>
<author>Erik Porter
  <title>Visual Basic .NET KickStart</title>
</author>

In the sample XML, the root element is book.
Empty elementsIf an Element does not contain another element or text, it is empty. Empty elements may be written in one of two forms:
<chapter></chapter>

or

<chapter />

This second form is a handy shortcut. Notice that the chapter elements in the XML file use this form. (Even though they include Attributes, they are still considered empty.)

This is one of the major differences between HTML and XML. If you have worked with HTML, you know that there is a <br> element that defines a line break. To use this properly in an XML document, this should be written <br />.
All elements must be properly nestedBoth the start and end tag of an element must be contained by the start and end tag of its parent. So, <book><title>Visual Basic .NET KickStart</book></title> is not proper as the close tag for title is not within the start and end tags for book. This type of sloppy coding is acceptable in HTML, however, as you could write something like <font face="Arial><b>Some Text</font></b> and most browsers would interpret it correctly.
Well-formedXML is well-formed if it follows all of these rules. Generally, you will need well-formed XML to use the tools in Visual Basic .NET.
ValidatedRather than use any tags in a document, it is often preferred to use a definition of the document. For XML, these are either DTDs (Document Type Definition) or XML Schema (the preferred form of defining documents) documents. These definitions describe the valid tags for a document, ordering of tags, and similar information about what the document should look like. In “real-world” systems, you will likely want to create one or more XML Schemas, defining the types of XML documents your application will process. This allows the various parts of your application to ensure compliance with those Schemas.

One thing to keep in mind as you work with XML: it is case-sensitive. When you are searching for, or using a particular Element or Attribute, the case must match the version in the document. So, Book, book, and BOOK are three different items as far as XML is concerned.

Listing 10.2. Sample XML File
<?xml version="1.0" encoding="utf-8"?>
<!—information about this book—>
<book isbn="0-672-32549-7">
    <title>Microsoft Visual Basic .NET 2003 Kick Start</title>
    <authors>
        <author>Duncan Mackenzie</author>
        <author>Andy Baron</author>
        <author>Erik Porter</author>
        <author>Joel Semeniuk</author>
    </authors>
    <chapters>
        <chapter id="1" topic="Introducing Visual Basic .NET" />
        <chapter id="2" topic="Language Changes" />
        <chapter id="3" topic="Building Windows Applications" />
        <chapter id="4" topic="Working with Files" />
        <chapter id="5" topic="Data Binding" />
        <chapter id="6" topic="Data, without the binding" />
        <chapter id="7" topic=" Object-Oriented Programming " />
        <chapter id="8" topic="Custom Controls" />
        <chapter id="9" topic="COM Interop" />
        <chapter id="10" topic="Advanced Topics" />
        <chapter id="Appendix" topic="Upgrade Tools" />
    </chapters>
</book>
					

How to Use XML in Your Applications

How does XML fit into your applications? Essentially, you can use it in almost any way you want; it is just a file format for storing information. XML can be used for data, configuration, and as a format for moving information between layers of a distributed application. For example, the configuration files used by both ASP.NET and Windows Forms applications are XML files. XML Web Services (as you will see later this chapter) uses XML as the format for moving information to the clients of the Web Service.

Visual Basic .NET provides several ways of directly working with XML, although many other activities (such as Web Services and Configuration files) also handle XML. This section takes a look at only one of the more common methods, using the DOM (Document Object Model) method. It is worth noting that there are also the reader/writer and serialize/deserialize methods.

For a Visual Basic 6 developer who has used MSXML before, using the DOM will seem quite natural. This is because both MSXML's DOMDocument, and Visual Basic .NET's System.Xml.DomDocument are both based on the recommended model for dealing with XML, as defined by the W3 organization (see http://www.w3.org).

For the DOM (either MSXML's DOMDocument object or System.Xml.DomDocument), an XML document is a collection of nodes. These nodes form a tree, with a single root node at the base. Each node is of a certain type; such as element, attribute, text, or comment. When writing, you create nodes, and add them to the tree. Reading works by walking the list of child nodes of the root, and then their child nodes, and so on, very similar to the way one would work with a directory tree on a hard drive.

Using DOM to Write Out XML

In order to demonstrate the use of the XMLDocument to read and write an XML file, Listing 10.3 shows the code used to write out the Book XML file shown earlier (in Listing 10.2).

Listing 10.3. XMLDocument Exposes the Node Hierarchy of an XML File
Dim myDoc As New Xml.XmlDocument

myDoc.AppendChild( _
    myDoc.CreateXmlDeclaration( _
    "1.0", "UTF-8", String.Empty))


myDoc.AppendChild( _
    myDoc.CreateComment( _
    "information about this book"))

Dim rootBook As Xml.XmlNode = _
myDoc.AppendChild( _
myDoc.CreateElement("book"))
rootBook.Attributes.Append( _
    myDoc.CreateAttribute("isbn"))
rootBook.Attributes( _
    "isbn").Value = "0-672-32549-7"

Dim title As Xml.XmlNode
title = rootBook.AppendChild( _
    myDoc.CreateElement("title"))
title.InnerText = _
    "Microsoft Visual Basic .NET 2003 Kick Start"
Dim authors As Xml.XmlNode _
    = rootBook.AppendChild( _
    myDoc.CreateElement("authors"))
Dim Duncan, Andy, _
    Erik, Joel As Xml.XmlNode

Duncan = authors.AppendChild( _
    myDoc.CreateElement("author"))
Andy = authors.AppendChild( _
    myDoc.CreateElement("author"))
Erik = authors.AppendChild( _
    myDoc.CreateElement("author"))
Joel = authors.AppendChild( _
    myDoc.CreateElement("author"))

Duncan.InnerText = "Duncan Mackenzie"
Andy.InnerText = "Andy Baron"
Erik.InnerText = "Erik Porter"
Joel.InnerText = "Joel Semeniuk"

Dim chapters As Xml.XmlNode _
= myDoc.CreateElement("chapters")

rootBook.AppendChild(chapters)

Dim chapterElements(10) As Xml.XmlElement

For i As Integer = 0 To 10
    chapterElements(i) = _
        myDoc.CreateElement("chapter")
    With chapterElements(i)
        .Attributes.Append( _
            myDoc.CreateAttribute("id"))
        .Attributes("id").Value = _
            CStr(i + 1)
        .Attributes.Append( _
            myDoc.CreateAttribute("topic"))
    End With
    chapters.AppendChild( _
        chapterElements(i))
Next


chapterElements(0).Attributes( _
    "topic").Value = _
    "Introducing Visual Basic .NET"
chapterElements(1).Attributes( _
    "topic").Value = _
    "Language Changes"
chapterElements(2).Attributes( _
    "topic").Value = _
    "Building Windows Applications"
chapterElements(3).Attributes( _
    "topic").Value = _
    "Working with Files"
chapterElements(4).Attributes( _
    "topic").Value = _
    "Data Binding"
chapterElements(5).Attributes( _
    "topic").Value = _
    "Data without the Binding"
chapterElements(6).Attributes( _
    "topic").Value = _
    "Object-Oriented Programming"
chapterElements(7).Attributes( _
    "topic").Value = _
    "Custom Controls"
chapterElements(8).Attributes( _
    "topic").Value = _
    "COM Interop"
chapterElements(9).Attributes( _
    "topic").Value = _
    "Advanced Topics"

chapterElements(10).Attributes( _
    "id").Value = _
    "Appendix"
chapterElements(10).Attributes( _
    "topic").Value = _
    "Upgrade Tools"

Dim myWriter As New _
    Xml.XmlTextWriter( _
    "C:	hisbook.xml", _
    System.Text.Encoding.UTF8)

myWriter.Formatting = _
    Xml.Formatting.Indented
myDoc.Save(myWriter)
myWriter.Close()
					

The code in Listing 10.3 creates an XML document one piece at a time, creating each element and then assigning attributes and text values as required. It takes quite a bit of code to work with XML files using the DOM, but none of that code is very complicated.

Reading XML Back In

Reading the sample book XML in using the XMLDocument is a bit easier than writing it out, mostly because you can do your reading in a few simple loops, but the general concept for reading is the same as for writing. Listing 10.4 shows how to use the XMLDocument class to read values out of an XML file.

Listing 10.4. Reading in XML Using XMLDocument Requires Traversing the Hierarchy of Nodes
Dim myDoc As New Xml.XmlDocument
myDoc.Load("c:	hisbook.xml")

Dim book As Xml.XmlElement
book = myDoc.DocumentElement()
Debug.WriteLine("ISBN: " _
    & book.Attributes("isbn").Value)

Dim title As Xml.XmlElement
title = book.Item("title")
Debug.WriteLine(title.InnerText)

Debug.WriteLine("Authors:")
Dim authors As Xml.XmlElement
authors = book.Item("authors")

For Each node As Xml.XmlNode _
    In authors.ChildNodes
    If node.Name = "author" Then
        Debug.WriteLine(node.InnerText)
    End If
Next

Debug.WriteLine("Chapters:")
Dim chapters As Xml.XmlElement
chapters = book.Item("chapters")

For Each node As Xml.XmlNode _
    In chapters.ChildNodes
    If node.Name = "chapter" Then
        Debug.WriteLine( _
            node.Attributes("id").Value)
        Debug.WriteLine( _
            node.Attributes("topic").Value)
    End If
Next
					

XPath and XSLT

This section barely scratched the surface of working with XML with Visual Basic .NET. As you scan the documentation, you will also hear of two other major XML standards—XPath and XSLT.

XPath is a way of querying XML documents. It is similar to querying a hard drive with a path in that it allows you to describe the navigation to a particular node in a document. Listing 10.5 shows how an XPath query could be used to retrieve all of the chapter numbers from the sample XML file.

Listing 10.5. XPath Is Somewhat Like SQL for XML, it Allows You to Query for the Portion of the XML Hierarchy You Are Interested In
Dim myDoc As New Xml.XmlDocument
myDoc.Load("c:	hisbook.xml")

Dim chpts As Xml.XmlNodeList = _
    myDoc.SelectNodes( _
    "/book/chapters/chapter")
For Each nd As Xml.XmlNode In chpts
    Debug.WriteLine( _
        nd.Attributes("id").Value)
Next
					

XSLT, or Extensible Stylesheet Language Transformations, is a means of converting an XML document to something else (different XML, HTML, text, and so on). In one sense, it is a programming language, just a rather different looking one. With XSLT, you define a number of templates that describe what to look for, and what to do once that is detected. When transforming, each of these templates replace the searched for text, and produce their output. Often used to transform XML into other formats of XML or into HTML for display, XSLT is a powerful, albeit slightly opaque tool in your XML workbench. You can either ignore it, or spend the time to learn it, as you choose.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.31.125