1. Home
  2. Computing & Technology
  3. Visual Basic

Visual Basic .NET 2008 Express - Using XML
Part 8 of an About Visual Basic Tutorial

By Dan Mabbutt, About.com

Jun 12 2008

This is a free tutorial to help beginning programmers get up to speed using Microsoft Visual Basic 2008 Express. To get the most from this tutorial, you might want to start at the beginning. The Complete Table of Contents for this course can be found on the first page of part 1: A "From the Ground Up" Tutorial.

About XML

In this segment of the tutorial, we continue to develop a more advanced version of the About Visual Basic Signature Block program using a different data technology, XML. Programmers have used EDI, parameter lists, .INI files, control strings, comma delimited files and a dozen other techniques to solving basically the same problem that we have in the Signature Block program: how to save a small collection of data between program executions. Today, almost all software technologies are in the process of converting to XML to do this. (That's not an exaggeration. They really are.)

If this is your first introduction to XML, you might think it's complicated and technical. Although XML can become very technical, it's also possible to be elegantly simple and easy to understand. We're going to aim for "simple and easy" here but just to make sure you have a complete view of things, we're also going to use some of the more complicated methods.

If you want the final, authoritative word on XML, check out the site of the World Wide Web Consortium (W3C). The W3C is the standards body for all things XML including XML itself and a growing body of satellite standards like schema, XSLT, XHTML and SOAP ... something we'll look at a little more later.

If you have ever looked at the HTML that is the source code used in Web pages, then you have seen basically the same concepts used by XML that have made the Web one of the outstanding success stories of our age. An XML file is basically just a text file, like HTML, where the data in the file is identified by tags or strings of text that are used to name the data. Tags are explained in more detail just a little later.

Traditionally, there were two ways that an XML file was processed. The DOM - the Document Object Model, which lets you process the whole XML document at one time, and SAX - Simple API for XML - which processes the XML as a stream of data much like a sequential file. In the new version of the Signature Block program that will be coded at the end of this article, the data is processed using the DOM. The DOM fits in well with the latest ADO.NET concept, the DataSet because they both assume that all the data a program will need has been read from storage and is waiting to be processed.

Microsoft has now added one more: LINQ - Language INtegrated Query. LINQ isn't limited to just XML but it has powerful features for making XML much more accessible.

We'll check out both the DOM and LINQ for accessing XML data in this segment.

The XML Advantage - Self Identifying Data

In the HTML language of a Web page, the information that displays on the page is identified with a tag. For example, if you want to give your Web page a title, you use the <title>My Web Page Title</title> tag. Because all operating systems can read simple text files and all browsers understand what the title tag does, any computer anywhere can interpret and use HTML. This universal compatibility is one of the biggest advantages of XML.

XML does exactly the same thing, but instead of using only pre-defined tags like "title", "img" (image), and "p" (paragraph), you can invent your own tags. In the XML that we will use, I invented the tags FName, FNameChecked, and so forth for the Signature Block program. The big difference with XML is that you also have to create the code to decide what to do with these tags. With HTML, that code is part of your browser and always does the same thing.

One advantage of XML is that it's readable by humans and computers. This is actually very important. To see why, note that in the part 7 version of our program, we had to write a utility program to create the initial version of the serialized file because it was a binary file and it's only practical for a computer to create that file. It's a little hard to create a binary file with Notepad. But with XML, you can use any text editor to create one. (Although there are many specialized XML editors available for more advanced applications.)

The only thing about the XML file that might seem mysterious is the XML declaration - the very first line of the file. There are a lot of technical things that can be done with an XML declaration, but for now, just use a declaration exactly like the one shown later and it will probably work for everything you will need. By the time you need anything more advanced, you will have learned enough to code one.

One final thing about XML ought to be kept in mind. It's not "OOP". By that, I mean that it's just data and you can't do something like XMLDoc.FName because FName isn't a property, it's an XML tag. It's the same with databases. If the data is in a relational database table, you can't code SQLTable.Fname for the same reason. (Until recently, this wouldn't have been important. But today, programmers are starting to expect everything to be OOP.)

This is one of the problems solved by the new .NET technology, LINQ. So we're going to code the Signature Block program using XML in two ways: the old, relatively complicated way and the new, simple way using LINQ. Click to go on to the next page and let's get started!

Explore Visual Basic
By Category
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Visual Basic
  4. Learn VB.NET
  5. Visual Basic .NET 2008 Express - Using XML

©2009 About.com, a part of The New York Times Company.

All rights reserved.