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」コレクションを保持する中間要素を追加する必要がありました。このようにして、私の外側のシーケンスは、両方とも「単純な」要素である 2 つの要素を保持します (ただし、実際にはそうではありません)。

<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 に答える