[英]How would I annotate this .net Type to serialise/deserialise to/from the following XML, using System.Xml.Serialization
方式:
public class Foo
{
public string Bar { get; set; }
public string Fred { get; set; }
}
实例
Foo x = new Foo { Bar = "Hi", Fred = "hey yourself" };
XML:
<Foo>
<Bar>Hi</Bar>
<John> <!-- Note extra layer -->
<Fred>hey there</Fred>
</John>
</Foo>
注意额外的John
标记,但没有为John
创建特殊类型。 如果我不能注释它,该如何以编程方式控制序列化过程。
尝试这个 :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Runtime;
using System.Runtime.InteropServices;
namespace ConsoleApplication42
{
class Program
{
static void Main(string[] args)
{
Foo x = new Foo { Bar = "Hi", Fred = "hey yourself" };
//<Foo>
// <Bar>Hi</Bar>
// <John> <!-- Note extra layer -->
// <Fred>hey there</Fred>
// </John>
//</Foo>
XElement foo = new XElement("Foo", new object[] {
new XElement("Bar", x.Bar),
new XElement("John", new XElement("Fred", x.Fred))
});
}
}
public class Foo
{
public string Bar { get; set; }
public string Fred { get; set; }
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.