Click here to Skip to main content
11,490,973 members (66,601 online)
Click here to Skip to main content

Tagged as

XamlServices class in .Net 4.0

, 28 Apr 2011 CPOL 8.8K 5
Rate this:
Please Sign up or sign in to vote.
Serializing and deserializing objects with XamlServices class
XAML has up to now played a big role in WPF and Silverlight and is seen as a markup language to design GUIs. That is not quite true. XAML describes objects, their properties and their relations. It is not limited to designing user interfaces. That is why XAML has been separated from WPF and Silverlight in .NET framework 4.0.

Let's assume you have a class BigSerializableObject and want to save it to a file with all properties and attached objects. Later, you want to be able to load it again from the file and get a new instance which is a perfect copy of the object you originally serialized. You might do this with XML. Serializing is not much of a problem. Deserializing is a completely different story. You would have to navigate through the XML nodes, create instances of the required objects, fill them with the proper values and assemble them according to the structure of the XML. That also means writing lots of code.

Things get far easier when using the System.Xaml.XamlServices class. Serializing the object is very similar to what you might be used to from XML:

using (TextWriter XamlWriter = File.CreateText("Test.xaml"))
{
  System.Xaml.XamlServices.Save(XamlWriter , MyBigSerializableObject);
}

That's it. Your object has just been serialized to XAML into the file Test.xaml. The only condition is that your object and all objects attached to it must be serializable.

Now we want to load our object again from the file Test.xaml. If possible, we want it as an object instance, just like it was before. And we don't want to write any code which constructs the object from the file. Try this:

using (TextReader XamlReader = File.OpenText("Test.xaml"))
{
  BigSerializableObject MyNewBigSerializableObject = (BigSerializableObject)System.Xaml.XamlServices.Load(XamlReader);
}

Again, that was it. You now have a new instance of your original object, loaded from the file.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

CDP1802

Germany Germany
No Biography provided

Comments and Discussions

 
GeneralThe graph structure is not preserved - sibling nodes are sor... Pin
WindowsPhone25-Oct-11 18:57
memberWindowsPhone25-Oct-11 18:57 
GeneralNB: For WPF content, you should use XamlReader instead of Xa... Pin
Richard Deeming4-May-11 9:55
memberRichard Deeming4-May-11 9:55 
GeneralNeed it for siverlight Pin
ELhajjNet9-May-11 4:06
memberELhajjNet9-May-11 4:06 
GeneralRe: Need it for siverlight Pin
CDP18029-May-11 4:19
memberCDP18029-May-11 4:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150520.1 | Last Updated 28 Apr 2011
Article Copyright 2011 by CDP1802
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid