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.
Fairly intuitive. But just liked it when it saw it in this book.
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