将对象序列化为xml我继承了一个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 propertiesstring xml = o.ToString();如何获得该对象的XML表示?
3 回答
有只小跳蛙
TA贡献1824条经验 获得超8个赞
XmlSerializer xsSubmit = new XmlSerializer(typeof(MyObject)); var subReq = new MyObject(); var xml = ""; using(var sww = new StringWriter()) { using(XmlWriter writer = XmlWriter.Create(sww)) { xsSubmit.Serialize(writer, subReq); xml = sww.ToString(); // Your XML } }
- 3 回答
- 0 关注
- 503 浏览
添加回答
举报
0/150
提交
取消