要收集的根元素的渲染禁用,则必须更换属性
[XmlArrayItem]用
[XmlElement]在你的代码。
要删除
xsi和
xsd名称空间,请创建一个
XmlSerializerNamespaces具有空名称空间的实例,并在需要序列化对象时将其传递。
看一下这个例子:
[XmlRoot("SHOPITEM")]public class ShopItem{ [XmlElement("PRODUCTNAME")] public string ProductName { get; set; } [XmlElement("VARIANT")] // was [XmlArrayItem] public List<ShopItem> Variants { get; set; }}// ...ShopItem item = new ShopItem(){ ProductName = "test", Variants = new List<ShopItem>() { new ShopItem{ ProductName = "hi 1" }, new ShopItem{ ProductName = "hi 2" } }};// This will remove the xsi/xsd namespaces from serializationXmlSerializerNamespaces ns = new XmlSerializerNamespaces();ns.Add("", "");XmlSerializer ser = new XmlSerializer(typeof(ShopItem));ser.Serialize(Console.Out, item, ns); // Inform the XmlSerializerNamespaces here
我得到以下输出:
<?xml version="1.0" encoding="ibm850"?><SHOPITEM> <PRODUCTNAME>test</PRODUCTNAME> <VARIANT> <PRODUCTNAME>hi 1</PRODUCTNAME> </VARIANT> <VARIANT> <PRODUCTNAME>hi 2</PRODUCTNAME> </VARIANT></SHOPITEM>
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)