我如何将对象序列化到一个XDocument? [英] How do I serialize an object into an XDocument?
本文介绍了我如何将对象序列化到一个XDocument?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个标有DataContract属性和我想创建该类的对象一个XDocument类。请告诉我这样做呢?
I have a class that is marked with DataContract attributes and I would like to create an XDocument from objects of that class. Whats the best way of doing this?
我可以通过一个XmlDocument要做到这一点,但这似乎是不必要的一步。
I can do it by going via an XmlDocument but this seems like an unnecessary step.
推荐答案
您可以直接创建的XmlWriter到的XDocument:
You can create an XmlWriter directly into the XDocument:
XDocument doc = new XDocument();
using (var writer = doc.CreateWriter())
{
// write xml into the writer
var serializer = new DataContractSerializer(objectToSerialize.GetType());
serializer.WriteObject(writer, objectToSerialize);
}
Console.WriteLine(doc.ToString());
这篇关于我如何将对象序列化到一个XDocument?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文