DataContractSerializer and ObservableCollection-s

Damir Dobric Posts

Next talks:

 

    

Follow me on Twitter: #ddobric



 

 

Archives

When using Silverlight, you may want to deserialize some data. Because XmlSerializer is not supported, you could peek DataContractSerializer (system.runtime.serialization.dll). In some specific scenario I had an observable list, which contains elements of some custom type named ServiceInfo.
Unfortunately, if you use the common serialization code shown below to serialize the (observable) list, following exception will be thrown:

image

Type 'ServiceInfo' with data contract name 'ServiceInfo:http://schemas.datacontract.org/2004/07/...' is not expected. Add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.

To fix this problem the underlying type used as list element has to be added as known type. Following code shows how to do that:

image


Posted Aug 04 2008, 02:24 PM by Damir Dobric

Comments

Mike wrote re: DataContractSerializer and ObservableCollection-s
on 03-27-2012 0:29

Thanks, this look useful - but I can't even get your code to compile.  Here's what I have:

DataContractSerializer ser = new DataContractSerializer(typeof(ObservableCollection<object>) new Type[] { typeof(CategoryModel) });

But VS2008 gives me the following error on the  "new Type[] " part: "Only assignment, call, increment, decrement, and new object expressions can be used as a statement"

Are you sure your syntax is correct and compiles?  

developers.de is a .Net Community Blog powered by daenet GmbH.