Reviewing the Goal of the Program
Let's review the goal of the program. I find that I often want to make small changes in the signature block that is automatically added in my email program. Using the signature block in a lot of tools, like newsgroups and forums, is very inconvenient. I want a "copy and paste" signature block that I can add to messages. What I need is a quick little program that will just display a visible block of text just before I send my message so the block will contain exactly what I want.
The version of the Signature Block program that we develop in this segment of the tutorial will start with the previous program as a basis. If you need the source code from that segment, download it here.
The problem we're working on with the Signature Block program is how best to save the data about which fields are displayed in the signature block. In part 7, the program saved the data in a file by serializing it. Serialized data is easy for computers to use. In part 7, all we had to do to create and then retrieve a file with our SigBlock data was ...
BinFormatter.Serialize(FS, MySigBlock)
mySigBlockStruct = BinFormatter.Deserialize(FS)
Although the file we created, SigBlockData.txt, was saved as a "text" file for convenience (It could have been a .bin file.) it's not like any text file you're used to seeing because it was created using the Binary Formatter of the Serialization object. If you display it in Notepad, you see something like this:
--------
Click Here to display the illustration
Click the Back button on your browser to return
--------
This is a good way to stash and retrieve the Signature Block structure, but it's not good for too much else. As useful as serialization is, XML is far more useful. We will create our own XML file by simply entering the text using Notepad, but to demonstrate how XML could be used instead of a binary serialized file, let's do that too.
Serializing to a SOAP XML file
.NET uses SOAP - Simple Object Access Protocol to serialize data to an XML file. In the previous example, a short program was included with the download that created the initial serialized file. Here's the code that creates the file (repetitive code isn't shown):
Imports System.IO
Imports System.RunTime.Serialization.Formatters
Public Class SigBlockForm
Private Sub SigBlockCreate_Click(ByVal sender ...
Dim MySigBlock As SigBlockStruct = New SigBlockStruct
Const SigBlockFile As String = "SigBlockData.txt"
Dim FS As New System.IO.FileStream _
(SigBlockFile, IO.FileMode.Create)
Dim BinFormatter As New Binary.BinaryFormatter()
MySigBlock.FName = "Dan"
MySigBlock.FNameChecked = True
...
BinFormatter.Serialize(FS, MySigBlock)
End Sub
End Class
To convert this code to use XML, just change the "binary" objects to "SOAP" objects. The objects that are changed are shown in italics.
Imports System.IO
Imports System.Runtime.Serialization.Formatters
Public Class SigBlockForm
Private Sub SigBlockCreate_Click(ByVal sender ...
Dim MySigBlock As SigBlockStruct = New SigBlockStruct
Const SigBlockFile As String = "SigBlockData.xml"
Dim FS As New System.IO.FileStream _
(SigBlockFile, IO.FileMode.Create)
Dim SoapFormatter As New Soap.SoapFormatter
MySigBlock.FName = "Dan"
MySigBlock.FNameChecked = True
...
SoapFormatter.Serialize(FS, MySigBlock)
End Sub
End Class
One additional detail is necessary for SOAP. VB.NET Express doesn't add the namespace for SOAP automatically to your project, so you also have to add a reference the SoapFormatter class. Click Add Reference in the Project menu and then select the System.Runtime.Serialization.Formatters.Soap component under the .NET tab.
The serialized XML file is shown in the illustration below. Instead of showing it in Notepad, however, I've used VB.NET 2008 Express. Just drag and drop the file into the main window.
Note that this time, the fields are identified by their names and the whole thing is packaged in an "envelope". The main goal of SOAP is to provide a standard envelope for XML information to be transmitted through network messages. It's a key part of Web services and Service-Oriented Architecture (SOA).
--------
Click Here to display the illustration
Click the Back button on your browser to return
--------
XML and SOAP are vendor neutral international standards created by the W3C organization. They are not Microsoft proprietary formats. The W3C is the organization that brought you HTML and the Web. So one big advantage right away is that you are using a technology that is not controlled just by Microsoft.
VB.NET 2008 also introduced another new way to create and use something called XML literals. I introduce that on the next page.

