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

Microsoft Visio 2010 : Serialization with XAML

6/13/2011 5:36:47 PM
XAML offers other advantages that can be taken in completely different scenarios; one of these is serialization. The System.Xaml.dll assembly implements the System.Xaml namespace that offers the XamlServices class whose purpose is providing members for reading and writing XAML in serialization scenarios. Because XAML is substantially Xml code that adheres to specific schemas, serialization output will be under Xml format. The good news is that you are not limited in using XAML serialization only in WPF applications. You simply need to add a reference to System.Xaml.dll. To understand how it works, create a new console project with Visual Basic and add the required reference. The goal of the code example is to understand how entire objects’ graphs can be serialized with this technique. 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
Public Property Friends As List(Of Person)
End Class

Other than the usual properties, it exposes a Friends property of type List(Of Person). This enables creating a simple object graph. Now consider the following code that creates two instances of the Person class that populates the Friends property of the main Person instance that we serialize:

Dim oneFriend As New Person With {.LastName = "White",
.FirstName = "Robert", .Age = 35}
Dim anotherFriend As New Person With {.LastName = "Red",
.FirstName = "Stephen", .Age = 42}

Dim p As New Person With {.LastName = "Del Sole", .FirstName = "Alessandro",
.Age = 32,
.Friends = New List(Of Person) _
From {oneFriend, anotherFriend}}


Using objects and collection initializers makes this operation straightforward. To serialize an object graph, you simply invoke the XamlServices.Save shared method that requires an output stream and the object to be serialized. The following code snippet demonstrates this:

Imports System.IO, System.Xaml
'...
Using target As New FileStream("C:\Temp\Person.xaml", FileMode.Create)
XamlServices.Save(target, p)
End Using

Serializing Generic Collections

When you serialize generic collections, especially custom ones, ensure that they implement the IList or IDictionary interfaces or the serialization process might not work correctly.


The previously described serialization process produces the following output:

<Person Age="32"
FirstName="Alessandro"
LastName="Del Sole"
xmlns="clr-namespace:XamlSerialization;assembly=XamlSerialization"
xmlns:scg="clr-namespace:System.Collections.Generic;
assembly=mscorlib"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

<Person.Friends>
<scg:List x:TypeArguments="Person" Capacity="4">
<Person Friends="{x:Null}" Age="35" FirstName="Robert"
LastName="White" />
<Person Friends="{x:Null}" Age="42" FirstName="Stephen"
LastName="Red" />
</scg:List>
</Person.Friends>
</Person>

This technique is efficient and makes output readable. As usual in Xaml files, the Xaml schema is pointed to via the x namespace. Notice how the scg namespace points to the System.Collections.Generic .NET namespace, required for deserializing the content as a generic collection. Also notice how the Person.Friends element defines subsequent Person elements storing information on child Person classes being part of the Friends property. Finally, notice how the Friends property for nested Person elements is null. (We did not define child elements for the property.) Deserializing such content is also straightforward. To accomplish this you simply invoke the XamlServices.Load shared method converting its result into the appropriate type. The following code shows how deserialization works, iterating the final result for demonstrating that deserialization was correctly performed:

Using source As New FileStream("C:\temp\person.xaml", FileMode.Open)
Dim result As Person = CType(XamlServices.Load(source), Person)

'Shows:
'White
'Green
For Each p In result.Friends
Console.WriteLine(p.LastName)
Next
Console.ReadLine()
End Using

XAML serialization can be used in different situations, such as persisting the state of WPF controls but also serializing entire .NET objects graphs.

Other -----------------
- Microsoft Visio 2010 : Custom Serialization
- Microsoft Visio 2010 : Connecting Shapes with Dynamic Connectors
- Microsoft Visio 2010 : Copying and Pasting Shapes & Connecting Shapes with Lines
- Microsoft PowerPoint 2010 : Working Together on Office Documents - Creating Office Documents on Windows Live
- Microsoft PowerPoint 2010 : Working Together on Office Documents - Setting Folder Permissions on Windows Live
- Visual Basic 2010 : XML Serialization
- 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
 
 
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