Logo
programming4us
programming4us
programming4us
programming4us
Windows XP
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server
programming4us
Windows Phone
 
 
programming4us
Windows 7

Visual Basic 2010 : XML Serialization

6/12/2011 4:30:17 PM

Note

Code examples shown in this section require Imports System.IO and Imports System.Xml.Serialization directives.


One of the main goals of serialization is to provide a way for exchanging data with other applications so that such applications can re-create objects’ state. If you want to share your objects with non-.NET applications or with applications running on different platforms, a convenient way for serializing objects is provided by the Xml serialization. As you know, Xml is a standard international file format for data exchange. Xml files are basically text files organized according to a hierarchical structure and therefore can be manipulated in whatever platforms and applications. Xml serialization thus provides two great benefits: absolute interoperability and background compatibility. If you upgrade or modify your applications, Xml format remains the same. Opposite to such benefits, Xml serialization has two limitations: It cannot serialize object graphs (therefore single objects) and cannot serialize private members. Xml serialization is performed by taking advantage of objects exposed by the System.Xml.Serialization namespace. Particularly you can use the XmlSerializer class that requires a System.IO.Stream object for outputting serialized data and the data itself. The following code shows how you can serialize a typed collection of strings using Xml serialization:

Dim stringSeries As New List(Of String) From
{"Serialization", "demo",
"with VB"}

Dim targetFile As New FileStream("C:\temp\SerializedData.xml",
FileMode.Create)
Dim formatter As New XmlSerializer(GetType(List(Of String)))

formatter.Serialize(targetFile, stringSeries)
targetFile.Close()
formatter = Nothing

The XmlSerializer constructor requires the specification of the data type you are going to serialize, which is accomplished via the GetType operator. To serialize data you invoke the XmlSerializer.Serialize method. As you can see, there are no big differences with other serialization techniques shown in the previous section. To check how your data was serialized, you can open the SerializedData.xml file. In this case you can accomplish this with an Xml editor or with a web browser instead of Notepad. Figure 1 shows the serialization result within Internet Explorer.

Figure 1. The Xml serialization result shown in Internet Explorer.

Notice how the newly obtained file has a perfect Xml structure and therefore can be shared with other applications having the ability of performing Xml deserialization. To deserialize your data you simply invoke the XmlSerializer.Deserialize method, as shown in the following code:

Dim sourceFile As New FileStream("C:\temp\SerializedData.xml",
FileMode.Open)

formatter = New XmlSerializer(GetType(List(Of String)))
Dim data = CType(formatter.Deserialize(sourceFile),
List(Of String))

sourceFile.Close()
formatter = Nothing

'Iterates the result
For Each item In data
Console.WriteLine(item)
Next

Customizing Xml Serialization

Consider the following implementation of the Person class:

Public Class Person
Public Property FirstName As String
Public Property LastName As String
Public Property Age As Integer
End Class

When you serialize an instance of that Person class, you would obtain an Xml representation similar to the following:

<?xml version="1.0" ?>
<Person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<FirstName>Alessandro</FirstName>
<LastName>Del Sole</LastName>
<Age>32</Age>
</Person>

The System.Xml.Serialization namespace offers attributes for controlling output of the Xml serialization to affect the target file. For example, see the following code:

Imports System.Xml.Serialization

<XmlRoot("Contact")> Public Class Person
<XmlIgnore()> Public Property FirstName As String
Public Property LastName As String
<XmlAttribute()> Public Property Age As Integer
End Class

When an instance is serialized, the output looks like the following:

<?xml version="1.0" ?>
<Contact xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
Age="32">
<LastName>Del Sole</LastName>
</Contact>

The XmlRoot attribute changed the name of the root element from Person to Contact. The XmlIgnore attribute prevented a property from being serialized, whereas the XmlAttribute attribute treated the specified member as an Xml attribute instead of an Xml element. You can find the complete attributes list in the dedicated page of the MSDN Library at http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlattributes_members(VS.100).aspx. The reason why you should get a reference on the Internet is that Xml serialization is a settled concept for most developers, whereas .NET Framework 4.0 provides a new, more interesting way for Xml serialization, known as XAML serialization.

Other -----------------
- Visual Basic 2010 : Objects Serialization (part 2) - Soap Serialization & Providing Serialization for Custom Objects
- Visual Basic 2010 : Objects Serialization (part 1) - Binary Serialization
- Microsoft Excel 2010 : Editing a Chart & Moving and Resizing a Chart
- Microsoft Excel 2010 - Choosing the Right Type of Chart & Creating a Chart
- Microsoft PowerPoint 2010 : Working Together on Office Documents - Working with Folders on Windows Live
- Microsoft PowerPoint 2010 : Working Together on Office Documents - Accessing Documents on Windows Live
- Microsoft Visio 2010 : Creating a New Diagram - Selecting Shapes
- Microsoft Visio 2010 : Creating a New Diagram - Using Basic Shapes and the Dynamic Grid
- Managing Security in Windows 7 : Windows Firewall
- Managing Security in Windows 7 : Designing BitLocker Support
 
 
Video tutorials
- How To Install Windows 8 On VMware Workstation 9

- How To Install Windows 8

- How To Install Windows Server 2012

- How To Disable Windows 8 Metro UI

- How To Change Account Picture In Windows 8

- How To Unlock Administrator Account in Windows 8

- How To Restart, Log Off And Shutdown Windows 8

- How To Login To Skype Using A Microsoft Account

- How To Enable Aero Glass Effect In Windows 8

- How To Disable Windows Update in Windows 8

- How To Disable Windows 8 Metro UI

- How To Add Widgets To Windows 8 Lock Screen
programming4us programming4us
Popular tags
Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Biztalk Exchange Server Microsoft LynC Server Microsoft Dynamic Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 windows Phone 7 windows Phone 8
programming4us programming4us
 
Popular keywords
HOW TO Swimlane in Visio Visio sort key Pen and Touch Creating groups in Windows Server Raid in Windows Server Exchange 2010 maintenance Exchange server mail enabled groups Debugging Tools Collaborating
programming4us programming4us
 
programming4us
Women
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server
programming4us
Windows Phone