C# 如何将属性添加到 XmlArray 元素(XML 序列化)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1052556/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I add a attribute to a XmlArray element (XML Serialization)?
提问by 123Developer
How do I add a attribute to a XmlArray element ( not to XmlArrayItem ) while serializing the object?
如何在序列化对象时将属性添加到 XmlArray 元素(而不是 XmlArrayItem )?
回答by Ray Lu
XmlArray is used to tell the xmlserializer to treat the property as array and serialize it according its parameters for the element names.
XmlArray 用于告诉 xmlserializer 将属性视为数组并根据元素名称的参数对其进行序列化。
[XmlArray("FullNames")]
[XmlArrayItem("Name")]
public string[] Names{get;set;}
will give you
会给你
<FullNames>
<Name>Michael Hymanson</Name>
<Name>Paris Hilton</Name>
</FullNames>
In order to add an xml attribute to FullNames element, you need declare a class for it.
为了向 FullNames 元素添加 xml 属性,您需要为其声明一个类。
[XmlType("FullNames")]
public class Names
{
[XmlAttribute("total")]
public int Total {get;set;}
[XmlElement("Name")]
public string[] Names{get;set;}
}
This will give you
这会给你
<FullNames total="2">
<Name>Michael Hymanson</Name>
<Name>Paris Hilton</Name>
</FullNames>