我有一个来自 TeamCity 的 xml 文件,看起来像这样(没有其他元素):
<builds count="2">
<build id="1" number="100" running="true" (etc) />
<build id="2" number="200" running="true" (etc) />
<build id="3" number="200" running="true" (etc) />
</builds>
我的数据合约如下所示:
[Serializable,DataContract(Name="builds", Namespace = "")]
public class builds
{
[XmlArray]
public build[] build;
[XmlAttribute("count")]
public int count { get; set; }
}
[Serializable, DataContract(Name = "build", Namespace = "")]
public class build
{
[XmlAttribute("id")]
public int id;
[XmlAttribute("number")]
public int number;
[XmlAttribute("running")]
public bool running;
[XmlAttribute("percentageComplete")]
public int percentageComplete;
[XmlAttribute("status")]
public string status;
[XmlAttribute("buildTypeId")]
public string buildTypeId;
[XmlAttribute("startDate")]
public string startDate;
[XmlAttribute("href")]
public string href;
[XmlAttribute("webUrl")]
public string webUrl;
}
很明显,如果这样可行,我就不会在这里了 :) 如何解决所有名称相同的顶级数组元素的序列化问题?如果我把它变成一个构建实例,它工作正常,我可以与对象交互。