Microsoft likes to say that their support for XML is deep and that certainly seems to be true. Another new way to create XML in VB.NET 2008 is called XML literals. (It's worth mentioning that XML literals are not available in other .NET languages such as C#!) Now, rather than creating XML in Notepad or something like that, you can create them directly in your source code. I mentioned this back in part 7. Now let's see what our SigBlock would look like when coded directly as an XML literal.
Dim SigBlockXML As XElement = _
<SigBlock>
<FName>Dan</FName>
<FNameChecked>True</FNameChecked>
<LName>Mabbutt</LName>
<LNameChecked>True</LNameChecked>
<Address>About Visual Basic</Address>
<AddressChecked>True</AddressChecked>
<Phone>(555) 555-5555</Phone>
<PhoneChecked>False</PhoneChecked>
<SSN>123-45-6789</SSN>
<SSNChecked>False</SSNChecked>
<Email>visualbasic@about.com</Email>
<EmailChecked>True</EmailChecked>
</SigBlock>
Console.WriteLine(SigBlockXML)
I should note here that this is hardly a model of XML coding. At a minimum, the "checked" elements would be XML "attributes" rather than elements in a more practical situation.
Using the XML 'DOM' - One Way to Solve the Problem
Now that we know a little bit about XML, I'll explain exactly what needs to be done to use one of the traditional ways of processing XML. To help you understand this explanation, the entire program source is shown below. You might want to open it in a separate window so you can refer to it when necessary. This is a large graphic and you'll have to expand it to full size and scroll to display different parts of it.
--------
Click Here to display the illustration
Click the Back button on your browser to return
--------
As a further demonstration of the value of OOP, the main object in our program, SigBlockForm, the SigFieldDisplay object, and the data structure SigBlockStruct can all remain completely unchanged when we recode our program to use an XML file instead of a serialized file. To visualize this type of advantage in the real world, suppose you had sold a million or so copies of this program and you wanted to upgrade it. With objects, all you have to do is provide the object that changes. This is how sophisticated systems like Microsoft's Windows Update work!
The SigBlock object that does the real work, however, changes completely! To read the XML file, we use a technique that is known by the name DOM, or Document Object Model. This isn't the only technology that could be used. Since XML is used in so many ways today, there are a lot of different ways to include XML into your program.
When XML is processed using the DOM, the entire XML file is treated as a single object and methods are provided that can access any part of it. This technique is extremely flexible, but it requires enough memory to hold the whole XML file at one time. It can be slow if you try to use it with large XML files because the file will probably have to be swapped into virtual memory. So the DOM is used most often with small XML files - just like the one we're using. Fortunately, that's most of the applications out there.
The XmlNodeReader Object
As we learned earlier, we put the code that will execute one time in the Load method of SigBlock. It starts by creating instances of the XML objects we'll need. XmlDocument, XmlNode, and XmlNodeReader are all objects provided by .NET in the System.XML namespace. The Load method of the XmlDocument class (which we have created in the mySigBlock object here) puts our entire XML file in memory.
Dim mySigBlock As XmlDocument = New XmlDocument()
Dim myNode As XmlNode
Dim myXmlNodeReader As XmlNodeReader
mySigBlock.Load(SigDocument)
We then select the SigBlock node of the XML document. Look at the XML document shown earlier to see that the SigBlock node contains all of the data we'll need.
myNode = mySigBlock.DocumentElement.SelectSingleNode("SigBlock")
We pass this XML node to the XMLNodeReader object next:
myXmlNodeReader = New XmlNodeReader(myNode)
XmlNodeReader will retrieve everything in the node and allow your program to act on any part of the node. Here's Microsoft's description:
An XmlNodeReader is a reader that provides fast, non-cached, forward-only access to XML data in an XmlNode.
The Read() method of XmlNodeReader is the operation that gets the next part of the node. At the end of the node, it returns a Boolean false value. So we can process the entire node with a Do While loop:
Do While (myXmlNodeReader.Read())
... other statements
Loop
Inside the loop, the program first checks to see if an XML element has been read. This is where our data is. There are a lot of different parts of the XML document that can be accessed using the Read() method so we have to make sure that we only use the ones we need.
If myXmlNodeReader.NodeType = XmlNodeType.Element Then
We then use the Name property of the element as the value to allow a Select Case statement to put the values in the XML into our data structure using the ReadString() method. Here's the first one:
Select Case myXmlNodeReader.Name
Case "FName"
mySigBlockStruct.FName = myXmlNodeReader.ReadString()
The GetLines and SigFieldsShow methods are, again, exactly the same. Only the way the data is placed into our data structure has been recoded. The ability to only change one part of a program and leave tested, functioning parts of the program alone is a primary advantage of VB.NET and Object Oriented Programming.
To download the source code for DOM version of the XML Signature Block program, click here.

