Alessandro Del Sole's Blog

/* A programming space about Microsoft® .NET® */
posts - 159, comments - 0, trackbacks - 0

My Links


Your host

This is me! This space is about Microsoft® .NET® and Microsoft® Visual Basic development. Enjoy! :-)

These postings are provided 'AS IS' for entertainment purposes only with absolutely no warranty expressed or implied and confer no rights.

Microsoft MVP

My MVP Profile

I'm a VB!

Watch my interview in Seattle

My new book on VB 2015!

Pre-order VB 2015 Unleashed Pre-order my new book "Visual Basic 2015 Unleashed". Click for more info!

My new book on LightSwitch!

Visual Studio LightSwitch Unleashed My book "Visual Studio LightSwitch Unleashed" is available. Click the cover!

Your visits

Follow me on Twitter!

CodePlex download Download my open-source projects from CodePlex!

Article Categories


Post Categories

.NET Framework


Help Authoring

Microsoft & MSDN

Setup & Deployment

Visual Basic 2005/2008/2010

XAML serialization with .NET 4.0 and VB 2010

To be honest the technique I'm going to describe in this blog post is not really new in .NET 4.0 (although I'm going to explain it with Visual Basic 2010), by the way the new Framework version introduces something new. First, there is a new assembly named System.Xaml.dll which is responsible for processing and parsing XAML code in WPF applications. Most of what was part of the WindowsBase.dll assembly about XAML, now is exposed by the new library which exposes a namespace named System.Xaml.


Now it's time to discuss the technique that entitles this blog post. During the years, serialization in the .NET development had some changes and improvements, with particular regard to technologies such as WCF and the Entity Framework. With that said, one of the most recent techniques is the Xaml Serialization which is the ability of serializing our data to XAML markup code; such technique offers several advantages: first, it allows serializing entire object graphs to Xml. XAML is basically Xml, thus it is portable. Second, the possibility of serializing WPF objects; last but not least, the possibility of using such technique against classic .NET objects even if outside WPF applications.


The following code has been written with the Visual Studio 2010 Beta 1 and I will show also some of the new language features in VB 2010. With that said, let's create a new Console application and add a reference to the System.Xaml.dll assembly (added by default in WPF applications).

Now suppose you have a Customer class defined as follows:


Public Class Customer

    'Using auto-implemented properties :-)

    Public Property CustomerID As String

    Public Property CompanyName As String

    Public Property Orders As List(Of Order)

End Class


The class exposes a Orders property of type List(Of Order). In this way we can simulate an object graph. The Order could be defined like this (a simplified implementation):


Public Class Order

    Public Property OrderID As Integer

    Public Property OrderDate As Date

    Public Property ShipAddress As String

End Class


Now we need some object instances for demo purposes. First, let's create two new instances of the Order class. Notice how the code takes advantage of object initializers for in-line initialization:


        Dim order1 As New Order With {.OrderDate = Date.Today, .OrderID = 1, .ShipAddress = "Somewhere"}

        Dim order2 As New Order With {.OrderDate = Date.Today, .OrderID = 2, .ShipAddress = "Somewhere else"}


Now let's create an instance of the Customer class and populate its Orders properties with a collection of Order. We can take advantage of a new VB 2010 feature, known as collection initializers so that we can initialize our collection when creating the instance:


        Dim cust As New Customer With {.CustomerID = "DELSO", .CompanyName = "Del Sole",

                                       .Orders = New List(Of Order) From {order1, order2}}


To serialize the cust object we need a stream (either on disk or in memory); then we can invoke the XamlServices.Save method, as shown here:


        'Imports System.Xaml, System.IO directives required


        Using target As New FileStream("C:\temp\CustomerData.xaml", FileMode.Create)

            XamlServices.Save(target, cust)

        End Using


Save requires the target stream and the object to serialize as arguments. If we investigate the content of the new .Xaml generated file, we'll get the following result:


<Customer CompanyName="Del Sole"






        <scg:List x:TypeArguments="Order" Capacity="4">

            <Order OrderDate="2009-10-11T00:00+02:00" OrderID="1" ShipAddress="Somewhere"/>

            <Order OrderDate="2009-10-11T00:00+02:00" OrderID="2" ShipAddress="Somewhere else"/>






Over than the usual Xml namespace pointing to the XAML schema, notice how the System.Collections.Generic namespace is also referred. This is because we used generics in our code. The serialization result is quite simple to understand; the object was serialized as XAML and referenced objects are represented by a Customer.Orders node, completing the graph serialization. Notice how the scg:List tag means that its content needs to be represented by a generic list when deserializing.


Deserializing XAML is also simple. You invoke the XamlServices.Load method, as in the following code:



        Using source As New FileStream("C:\temp\CustomerData.xaml", FileMode.Open)

            'Conversion required, the method returns Object

            Dim myCustomer As Customer = CType(XamlServices.Load(source), Customer)


            For Each ord In myCustomer.Orders



        End Using



The For..Each loop has the only purpose of demonstrating that the deserialization process was successful. Only one notice about generics: if you serialize generics, remember that they must implement at least IList or IDictionary.

Of course this is just a small example but this technique can be also very complex. Surely it opens to new important scenarios, especially if considered within WPF applications.



Print | posted on domenica 18 ottobre 2009 01:45 | Filed Under [ Visual Basic Windows Presentation Foundation ]


No comments posted yet.

Post Comment

Please add 4 and 7 and type the answer here:

Powered by:
Powered By Subtext Powered By ASP.NET