在XSD中表示重复的一对XML元素

时间:2013-10-24 17:22:42

标签: c# xml xsd xml-serialization xsd.exe

我目前遇到XSD问题。通常情况下,条目如下所示:

<Entry Num="4">
    <Info>
        <Name>Something</Name>
        <ID>1234</ID>
        <Start>2013-01-07</Start>
        <Stop>2013-01-09</Stop>
        <Completed>6</Completed>
    </Info>
</Entry>

但偶尔看起来会像这样:

<Entry Num="5">
    <Info>
        <Name>SomethingElse</Name>
        <ID>5678</ID>
        <Start>2013-01-08</Start>
        <Stop>2013-01-10</Stop>
        <Start>2013-01-11</Start>
        <Stop>2013-01-12</Stop>
        <Completed>14</Completed>
    </Info>
</Entry>

为了尝试捕获多个Starts和Stops的潜力,我尝试了以下方法:

<xs:sequence maxOccurs="unbounded">
    <xs:element name="Start" type="xs:dateTime" maxOccurs="1"/>
    <xs:element name="Stop" type="xs:dateTime" maxOccurs="1"/>
</xs:sequence>

<xs:sequence maxOccurs="unbounded">
    <xs:element name="Start" type="xs:dateTime" />
    <xs:element name="Stop" type="xs:dateTime" />
</xs:sequence>

<xs:sequence maxOccurs="unbounded">
    <xs:sequence>
        <xs:element name="Start" type="xs:dateTime" />
        <xs:element name="Stop" type="xs:dateTime" />
    </xs:sequence>
</xs:sequence>

<xs:sequence maxOccurs="unbounded">
    <xs:sequence>
        <xs:element name="Start" type="xs:dateTime" maxOccurs="1"/>
        <xs:element name="Stop" type="xs:dateTime" maxOccurs="1"/>
    </xs:sequence>
</xs:sequence>

但是当我使用序列化为xsd.exe的xsd.exe将它转换为C#类时,它们都会生成一系列Starts,然后打印一个Stops数组:

<Entry Num="5">
    <Info>
        <Name>SomethingElse</Name>
        <ID>5678</ID>
        <Start>2013-01-08</Start>
        <Start>2013-01-11</Start>
        <Stop>2013-01-10</Stop>
        <Stop>2013-01-12</Stop>
        <Completed>14</Completed>
    </Info>
</Entry>

这与XML文件不匹配。有谁知道怎么做这样的事情吗?非常感谢。

我提出了一个有效的解决方案,但并不理想。

当前解决方案:

<xs:choice minOccurs="2" maxOccurs="unbounded">
    <xs:element name="Start" type="xs:dateTime"/>
    <xs:element name="Stop" type="xs:dateTime"/>
</xs:choice>

1 个答案:

答案 0 :(得分:1)

您只是错过了/order参数。

尝试这样的事情:xsd / c / order your.xsd

凭借额外的订单价值,输出可与您拥有的产品区分开来:

[System.Xml.Serialization.XmlElementAttribute("Start", typeof(System.DateTime), DataType="date", Order=2)]
[System.Xml.Serialization.XmlElementAttribute("Stop", typeof(System.DateTime), DataType="date", Order=2)]
[System.Xml.Serialization.XmlChoiceIdentifierAttribute("ItemsElementName")]
public System.DateTime[] Items {
    get {
        return this.itemsField;
    }
    set {
        this.itemsField = value;
    }
}

像这样的简单测试程序可以正确地往返XML:

using System;
using System.IO;
using System.Xml.Serialization;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            XmlSerializer ser = new XmlSerializer(typeof(Entry));
            Entry o;
            using (Stream s = File.OpenRead(@"D:\...\representing-a-repeated-pair-of-xml-elements-in-xsd-2.xml"))
            {
                o = (Entry)ser.Deserialize(s);
            }
            using (Stream s = File.OpenWrite(@"D:\...\representing-a-repeated-pair-of-xml-elements-in-xsd-3.xml"))
            {
                ser.Serialize(s, o);
            }
        }
    }
}