【问题标题】:SVCUTIL Error when exporting XSD element - maxoccurs must = 1导出 XSD 元素时出现 SVCUTIL 错误 - maxoccurs 必须 = 1
【发布时间】:2013-02-21 04:43:50
【问题描述】:

我在使用 Svcutil 将 XSD 转换为 C# 对象时遇到了一个非常奇怪的错误。

这是我的 XSD

<xs:element name="TestResults">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="TestResult" type="TestResultType" minOccurs="0" maxOccurs="unbounded"/>
            <xs:element name="atoken" type="IdentifierType"/>
        </xs:sequence>
    </xs:complexType>
</xs:element>

当我运行 Svcutil 时,我得到的错误是

D:\CambridgeAssessment\Documents\CA\BeaconSchemas-20130211>svcutil /dconly testResults.1.0.xsd

Error: Type 'TestResults' in namespace 'http://ucles/schema/ukba/TestResults/1/0
' cannot be imported. 'maxOccurs' on element 'TestResult' must be 1. Either chan
ge the schema so that the types can map to data contract types or use ImportXmlT
ype or use a different serializer.


If you are using the /dataContractOnly option to import data contract types and
are getting this error message, consider using xsd.exe instead. Types generated
by xsd.exe may be used in the Windows Communication Foundation after applying th
e XmlSerializerFormatAttribute attribute on your service contract. Alternatively
, consider using the /importXmlTypes option to import these types as XML types t
o use with DataContractFormatAttribute attribute on your service contract

如果我设置 'TestResult' 属性 'maxOccurs' = 1,那么一切正常。如果我完全删除 'atoken' 元素,它也适用于 'TestResult' 属性 'maxOccurs' = 'unbounded'。

查看DataContractSerializer 的架构引用,我发现以下内容:

<xs:element> can occur in the following contexts:

It can occur within an <xs:sequence>, which describes a data member of a regular (non-collection) data contract. In this case, the maxOccurs attribute must be 1. (A value of 0 is not allowed).

It can occur within an <xs:sequence>, which describes a data member of a collection data contract. In this case, the maxOccurs attribute must be greater than 1 or "unbounded".

因此,看起来在我的特定 XSD 中,Svcutil 认为 BOTH 元素都应该具有 'maxOccurs'=1,即使是集合的元素也是如此。

这种行为正确吗?还是我做错了什么?

【问题讨论】:

    标签: xsd svcutil.exe complextype


    【解决方案1】:

    感谢有关如何解决问题的建议。

    经过各种尝试,下面是我终于让它工作的方法。我不得不添加一个包含我的“TestResult”集合的中间元素。这样,我的外部序列包含两个都是“简单”元素的元素(尽管它们不是真的)

    <xs:element name="TestResults">
      <xs:complexType>
        <xs:sequence>
          <xs:element name="Token" type="IdentifierType" minOccurs="1" maxOccurs="1"/>
          <xs:element name="TestResultList">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="TestResult" type="TestResultType" minOccurs="0" maxOccurs="unbounded"/>
              </xs:sequence>
            </xs:complexType>
          </xs:element>
        </xs:sequence>
      </xs:complexType>
    </xs:element>
    

    【讨论】:

    • 但是你添加了新元素
    【解决方案2】:
    <?xml version="1.0" encoding="UTF-8"?> 
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">   
    <xs:element name="TestResults" >
    <xs:complexType>
    <xs:sequence>
        <xs:choice minOccurs="0" maxOccurs="unbounded">
            <xs:element name="TestResult" type="xs:string"/>
            <xs:element name="atoken" type="xs:string"/>
        </xs:choice>
    </xs:sequence>
    </xs:complexType>
    </xs:element>
    </xs:schema>
    

    请看 XSD - how to allow elements in any order any number of times?

    【讨论】:

    • 谢谢,但我担心这两个选项都不起作用。在选项 1 中,我收到一条错误消息,指出根上的“maxOccurs”必须为 1。如果我更改为 ,它会返回到“TestResult”的原始错误.对于选项 2 ,我收到一条错误消息,指出“所有”组中的 maxOccurs 必须为 0 或 1。
    • 恐怕您建议的替代方案也行不通。我收到一个错误,似乎表明“TestResults”作为根元素不能有嵌套序列:错误:无法导入命名空间“”中的“TestResults”类型。根序列必须只包含局部元素。不支持组引用、选择、任何和嵌套序列。要么更改架构,以便类型可以映射到数据协定类型,要么使用 ImportXmlType 或使用不同的序列化程序。
    • @maurocam 你能把整个架构贴在这里吗?
    • 很抱歉不能发布整个架构,因为它是机密的。需要时间来呈现它的通用性。关键是'TestResult'是'TestResultType'的类型化集合,而不是你所说的'xs:string'。
    • @maurocam 我想;如果您已尝试上述所有更新;如果仍然无法正常工作,那么问题不在于 XSD。需要检查解析器和 XML。最后,我添加了字符串来验证我这边的 XSD,在测试时您可以将其更改为您的。再见!
    猜你喜欢
    • 2020-08-21
    • 1970-01-01
    • 2017-03-31
    • 2014-11-01
    • 2011-08-08
    • 1970-01-01
    • 1970-01-01
    • 2018-10-23
    • 1970-01-01
    相关资源
    最近更新 更多