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

Visual Basic 2010 : XML Serialization

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
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
 
 
Top 10
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
 
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server