"Serialize" a DependencyObject - Rudi Grobler

"Serialize" a DependencyObject

In my previous post, I mentioned that my biggest gripe with deriving from DependencyObject is that it is not serializable! I stand corrected (Well, almost).

This is the code I tried initially

BinaryFormatter serializer = new BinaryFormatter();
serializer.Serialize(stream, Assesments);

And here is my exception

Type 'MyHealth.Model.AssesmentCollection' in Assembly 'MyHealth, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable.

To solve this problem, I tried the XamlWritter & XamlReader, here is the code

XamlWriter.Save(Assesments, stream);

THIS WORKED!!!

And now the code is plain and simple...

Download the newest version of myHealth at CodePlex

If you found this article useful, please kick it on DotNetKicks.com

Published Tuesday, June 24, 2008 7:58 PM by rudi

Comments

#

You've been kicked (a good thing) - Trackback from DotNetKicks.com

Tuesday, June 24, 2008 8:02 PM by DotNetKicks.com

# re: "Serialize" a DependencyObject

Unfortunately XamlWriter is sloooooow.  Try serializing several thousand DOs with it, and you won't be such a happy camper!  :)

Josh

Tuesday, June 24, 2008 8:07 PM by Josh Smith

# re: "Serialize" a DependencyObject

Uh, why were you trying to use binary serialization on a WPF object anyhow?  They are born for xaml serialization, after all.

Tuesday, June 24, 2008 10:10 PM by Will

# re: "Serialize" a DependencyObject

Josh, yes thy are slow but it was enough for my scenario... only had to serialize a few properties!!!

Will, read the previous article... I was forced to use DO's in my scenario... and I had to persist the information in these objects!!!

Wednesday, June 25, 2008 6:16 AM by rudi

# re: "Serialize" a DependencyObject

why XamlReader.Load dont work as well as XamlWriter.Save?????

The save method is very slow but it works ever. On complex classes the method Load dont work fine, is better to use [Serializable] classes.

Wednesday, June 25, 2008 10:08 AM by Juan Carlos Febrer

# Link Listing - June 25, 2008

Link Listing - June 25, 2008

Thursday, June 26, 2008 6:54 AM by Christopher Steen

# Link Listing - June 25, 2008

ASP.NET Using routing with ASP.NET web forms apps [Via: luisabreu ] WPF myHealth [Via: rudi ] "Serialize"...

Thursday, June 26, 2008 6:55 AM by Christopher Steen