I'm attempting to implement a DataContractSerializer
to generate an XML file that represents our somewhat complicated object model. The root object has multiple ICollection
properties, one of which has multiple ICollection
properties, multiples of which have multiple ICollection
properties... you get the idea.
I decorated all my relevant classes with [DataContract(Name = "foo")]
tags, read this question about using Include()
, and started framing it out. As I put together the top layer, I wondered if the second layer would need explicit declarations as well. Here's what I have so far:
public void Serialize(string DataCode)
{
Product prod = context.Products
.Include(p => p.Products)
.Include(p => p.References)
.Include(p => p.Dates)
.Include(p => p.Weights)
.Include(p => p.Notes)
.Include(p => p.Rules) // Rules have PriceConditions, which have Prices...
.Include(p => p.DataBooks)
.First(m => m.ProductCode == DataCode);
FileStream fs = new FileStream(path,FileMode.Create);
XmlDictionaryWriter writer = XmlDictionaryWriter.CreateTextWriter(fs);
DataContractSerializer serializer = new DataContractSerializer(typeof(Product));
serializer.WriteObject(writer, prod);
}
So, do I need to do something with Rules in order to ensure that the entire structure gets written, or does Include()
know to tunnel deep into the structure and load each element?