【问题标题】:Representing a repeated pair of XML elements in XSD在 XSD 中表示一对重复的 XML 元素
【发布时间】:2013-10-24 17:22:42
【问题描述】:

我目前遇到了 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>

为了尝试捕捉多次启动和停止的潜力,我尝试了以下方法:

<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 将其转换为 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>

【问题讨论】:

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


    【解决方案1】:

    您只是错过了/order 参数。

    试试这样的:xsd /c /order your.xsd

    输出可以通过额外的 Order 值与您的输出区分开来:

    [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);
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-12
      • 2011-05-06
      • 2015-08-19
      • 1970-01-01
      相关资源
      最近更新 更多