在数据协议中,有一个属性是字符串数组,
1 [System.Xml.Serialization.XmlArrayAttribute(Order = 19)] 2 [System.Xml.Serialization.XmlArrayItemAttribute("CardNumber", typeof(string), IsNullable = false)] 3 [DataMember] 4 public string[] Cards {get; set; }
我想把它序列化成下面的格式
<Cards>
<CardNumber>123123</CardNumber>
</Cards>
但是转换之后变成了
<Cards> <string>123123</string> </Cards>
解决方法是:
1.创建一个类代替数组
[CollectionDataContract(ItemName="CardNumber")] public class CardsList : List<string> { }
2.用
[DataMember(Name="Cards")] public CardsList Cards {get; set; };
替换
[DataMember] public string[] Cards {get; set; }
注:转自https://stackoverflow.com/questions/37586165/serializing-data-using-xmlarrayitemattribute-not-working-well