使用Svcutil將XSD轉換爲C#對象時遇到了一個非常奇怪的錯誤。導出XSD元素時出現SVCUTIL錯誤 - maxoccurs must = 1
這裏是我的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'一起使用。
縱觀架構refernce爲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,即使是一個這是一個集合。
此行爲是否正確?或者我做錯了什麼?
謝謝,但我怕兩個選項的工作都不是。在選項1中,我得到一個錯誤,說root上的'maxOccurs'必須是1.如果我更改爲,它會返回到我原來的錯誤'TestResult' 。對於選項2 我收到一個錯誤,說'全部'組中的maxOccurs必須是0或1. –
maurocam
2013-02-12 18:04:24
@maurocam更新了答案。 – 2013-02-13 04:53:52
恐怕你建議的替代方案也行不通。我收到一個錯誤,它似乎表明'TestResults'作爲根元素不能嵌套序列:錯誤:類型'TestResults'在命名空間''中無法導入。根序列必須只包含本地元素。組 參考,選擇,任何和嵌套序列不受支持。要麼更改模式,以便類型可映射到數據合約類型,或使用ImportXmlType或使用不同的序列化程序。 – maurocam 2013-02-13 09:51:11