2013-02-12 94 views
1

使用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,即使是一个这是一个集合。

此行为是否正确?或者我做错了什么?

回答

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 - how to allow elements in any order any number of times?

+0

谢谢,但我怕两个选项的工作都不是。在选项1 中,我得到一个错误,说root上的'maxOccurs'必须是1.如果我更改为,它会返回到我原来的错误'TestResult' 。对于选项2 我收到一个错误,说'全部'组中的maxOccurs必须是0或1. – maurocam 2013-02-12 18:04:24

+0

@maurocam更新了答案。 – 2013-02-13 04:53:52

+0

恐怕你建议的替代方案也行不通。我收到一个错误,它似乎表明'TestResults'作为根元素不能嵌套序列:错误:类型'TestResults'在命名空间''中无法导入。根序列必须只包含本地元素。组 参考,选择,任何和嵌套序列不受支持。要么更改模式,以便类型可映射到数据合约类型,或使用ImportXmlType或使用不同的序列化程序。 – maurocam 2013-02-13 09:51:11

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> 
+0

但您添加了新元素 2013-02-13 14:17:51

相关问题