How to Parse DataContract object to xml?
I define some class as 'DataContract'
I need to save this object in my database ( as xml ) before i sending it to the client.
When 开发者_如何转开发the .net framework send this object to the client - the object parse to xml - so i want to hold this xml and add it to my Database
How can i do it ? How can i get this xml without / before the sending to the client ?
You can use the DataContractSerializer in System.Runtime.Serialization to get an XDocument
XDocument doc = new XDocument();
using (XmlWriter writer = doc.CreateWriter())
{
DataContractSerializer serializer = new DataContractSerializer(typeof(T));
serializer.WriteObject(writer, obj);
writer.Close();
}
return doc;
where obj is your object and T is the type of your object
精彩评论