Serializing EntityCollections

I recently replied to a question about serializing entity collection. The problem they were having was not getting values of entity attributes serialized when using XmlSerializer. So the collection of attributes would look something like the following

Using DataContractSerializer though you can get the EntityCollection and the entity, and it's attributes to serialize using the following code

 EntityCollection ec = new EntityCollection();
 Entity e = new Entity("account");
 e["name"] = "dave,llc";
 DataContractSerializer dc = 
     new DataContractSerializer(typeof(EntityCollection));
 MemoryStream ms = new MemoryStream();
 dc.WriteObject(ms, ec);
 ms.Position = 0;
 StreamReader sr = new StreamReader(ms);
 var data = sr.ReadToEnd();

As you can see in the following image using DataContractSerializer you get the values for the attributes.

comments powered by Disqus