本文介绍了如何添加一个属性到XmlArray元素(XML序列化)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何添加一个属性到XmlArray元(不XmlArrayItem),而序列化对象?

How do I add a attribute to a XmlArray element ( not to XmlArrayItem ) while serializing the object?

推荐答案

XmlArray是用来告诉XmlSerializer的对待财产阵列,并根据其参数的元素名称序列。

XmlArray is used to tell the xmlserializer to treat the property as array and serialize it according its parameters for the element names.

[XmlArray("FullNames")]
[XmlArrayItem("Name")]
public string[] Names{get;set;}

会给你

<FullNames>
    <Name>Michael Jackson</Name>
    <Name>Paris Hilton</Name>
</FullNames>

为了到XML属性添加到FullNames元素,你需要声明一个类吧。

In order to add an xml attribute to FullNames element, you need declare a class for it.

[XmlType("FullNames")]
public class Names
{
   [XmlAttribute("total")]
   public int Total {get;set;} 
   [XmlElement("Name")]
   public string[] Names{get;set;}
}

这会给你

<FullNames total="2">
    <Name>Michael Jackson</Name>
    <Name>Paris Hilton</Name>
</FullNames>

这篇关于如何添加一个属性到XmlArray元素(XML序列化)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 23:49