1

使用 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 认为这两个元素都应该有 'maxOccurs'=1,即使是一个集合也是如此。

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

4

2 回答 2

0

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

经过各种尝试,下面是我终于让它工作的方式。我不得不添加一个包含我的“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>
于 2013-02-13T14:08:29.770 回答
0
<?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 - 如何允许任意顺序的元素任意多次?

于 2013-02-12T16:04:54.500 回答