3

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

1 回答 1

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);
            }
        }
    }
}
于 2013-10-25T01:01:39.180 回答