var store = GetStore(); using (IsolatedStorageFileStream fileStream = store.OpenFile(RootData, FileMode.Create)) { DataContractSerializer serializer = new DataContractSerializer(typeof(List)); serializer.WriteObject(fileStream, rootdatalist); }
But this only serializes the rootdatalist, and not the subitems. The rootdatalist has a nodes List property, how do I serialize that, so that I get the list hierarchy serialized?
Since it's dbml generated objects the Nodes property of Root is
public System.Data.Linq.Table<Node> Nodes
{
get
{
return this.GetTable<Node>();
}
}
My Datacontext return is:
public List<Root> GetRootList(Guid userid)
{
DataLoadOptions loadopts = new DataLoadOptions();
loadopts.LoadWith<Root>(s => s.Nodes);
this.DataContext.LoadOptions = loadopts;
return this.DataContext.Root.Where(s => s.Nodes.Count(n => n.UserId == userid) > 0).ToList();
}
the Node entityset looks as follows in my dbml designer
[global::System.Data.Linq.Mapping.AssociationAttribute(Name="Root_Node", Storage="_Nodes", ThisKey="Id", OtherKey="RootId")]
[global::System.Runtime.Serialization.DataMemberAttribute(Order=5, EmitDefaultValue=false)]
public EntitySet<Node> Nodes
{
get
{
if ((this.serializing && (this._Nodes.HasLoadedOrAssignedValues == false)))
{
return null;
}
return this._Nodes;
}
set
{
this._Nodes.Assign(value);
}
}
Also I have to have the [Include]
tag above my properties or nothing will be loaded.
Edit::
To others wanting to serialize dbml classes Link
Can you include any more information about the contract types? I would expect, for example, that Root
is marked as a data-contract, with the member as a data-member, for example:
[DataContract]
public class Root {
[DataMember]
public List<SubItem> Nodes {get;private set;}
}
[DataContract]
public class SubItem {
[DataMember]
public int Foo {get;set;}
[DataMember]
public string Bar {get;set;}
}
That should then work. If not, it would really help to see your types (or a cut-down version of them, that illustrates the problem).
The DataContractSerializer
needs to know about all of the types in your object graph.
Use the constructor overload that lets you specify those as well as the root type:
DataContractSerializer serializer = new DataContractSerializer(typeof(List<Root>), listOfOtherTypes);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With