我有一个C#类我已经继承了它。我已经成功地“构建”了这个对象。但是我需要将这个对象序列化为XML。有没有简单的方法可以做到这一点?
看起来这个类已经被设置为可序列化,但我不确定如何获得XML表示。我的类定义如下:
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.domain.com/test")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://www.domain.com/test", IsNullable = false)]
public partial class MyObject
{
...
}
这是我认为我可以做的,但它不起作用:
MyObject o = new MyObject();
// Set o properties
string xml = o.ToString();
我如何获得该对象的XML表示?