One technology .NET uses for XML is called SOAP It used to stand for Simple Object Access Protocol, but since the standard has been expanded, it's just a name now. 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 - see the code in the download for the complete program.)
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 SOAP, 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 (a reference to the code library) 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 this time, I've used VB.NET 2010 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". A number of namespaces are defined for more advanced uses of SOAP. 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) so the extra information is just there for those more advanced uses.
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 standard technology that is not controlled only by Microsoft.
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. Now, rather than creating XML in Notepad and then reading them as a file, you can create them directly in your source code. Let's see what our SigBlock would look like when coded directly as an XML literal. (You could add this code directly into a VB.NET project if you wanted to check it out.)
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>email@example.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.