Sunday, February 27, 2011

.NET Nugget : Viewing the serialized output of a DataContract on the console

Here are the steps (call it denotes sample variable names):
  • Create a DataContractSerializer of the type of the DataContract class (call it dcs)
  • Create a StringBuilder (call it sb)
  • Create an XmlWriter from the string builder ( call it writer)
  • use the dataContractSerializer.WriteObject to write the item instance to the xmlwriter. 
  • Print the sb.ToString to the console.

Heres the code: 

DataContractSerialzer dcs = new DataContractSerialzer(typeof(DataContractClass));
StringBuilder sb = new StringBuilder();
using(XmlWriter writer = new XmlWriter(sb))
{
   dcs.WriteObject(writer,dataContractClassInstance);
}
Console.WriteLine(sb.ToString());
Enjoy!

No comments:

Post a Comment