1

我有一个来自 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;
    }

很明显,如果这样可行,我就不会在这里了 :) 如何解决所有名称相同的顶级数组元素的序列化问题?如果我把它变成一个构建实例,它工作正常,我可以与对象交互。

4

1 回答 1

1

跟风怎么办

    [Fact]
    public void Test()
    {
        var builds = new builds
            {
                build = new[]
                    {
                        new build
                            {
                                buildTypeId = "1",
                                number = 100,
                                running = true
                            },
                        new build
                            {
                                buildTypeId = "2",
                                number = 200,
                                running = true
                            }
                    }
            };

        var namespaces = new XmlSerializerNamespaces();
        namespaces.Add(string.Empty, string.Empty);
        var writer = new StringWriter();
        new XmlSerializer(typeof(builds)).Serialize(writer, builds, namespaces);
        string result = writer.ToString();
        Console.Write(result);
    }

    [XmlRoot("builds")]
    public class builds
    {
        [XmlElement("build")]
        public build[] build;

        [XmlAttribute("count")]
        public int count { get; set; }
    }

    public class build
    {
        [XmlAttribute("buildTypeId")]
        public string buildTypeId;

        [XmlAttribute("href")]
        public string href;

        [XmlAttribute("id")]
        public int id;

        [XmlAttribute("number")]
        public int number;

        [XmlAttribute("percentageComplete")]
        public int percentageComplete;

        [XmlAttribute("running")]
        public bool running;

        [XmlAttribute("startDate")]
        public string startDate;

        [XmlAttribute("status")]
        public string status;

        [XmlAttribute("webUrl")]
        public string webUrl;
    }

结果输出:

<?xml version="1.0" encoding="utf-16"?>
<builds count="0">
  <build buildTypeId="1" id="0" number="100" percentageComplete="0" running="true" />
  <build buildTypeId="2" id="0" number="200" percentageComplete="0" running="true" />
</builds>
于 2013-12-31T19:15:16.160 回答