XML Schema:此上下文中不支持complexType元素

时间:2014-07-10 14:36:56

标签: c# xml xsd

我正在写一个.xsd文件,以便生成一个我可以从中导出xml的类。我已经按照http://www.w3schools.com/schema/的教程进行了操作,但我似乎无法弄清楚其中的一部分。 我想声明我自己的复杂类型(allocinfo)以用于多个元素。我无法弄清楚的是在我的架构中声明它的位置。我在下面第一次使用它之前已经声明它,但它给了我一个错误,它在这种情况下不可用。我也尝试在模式的开头声明它,但是我的嵌套元素无法看到它。

<xs:element name="Payload">
<xs:complexType>
  <xs:sequence>
    <xs:element name ="Alloc">
      <xs:complexType>
        <xs:sequence>

          <xs:complexType name="allocinfo"> //custom type
            <xs:sequence>
              <xs:element name="firstInfo"/>
              <xs:element name="secondInfo"/>
            </xs:sequence>
          </xs:complexType>

          <xs:element name="ID" type="xs:allocinfo"/> //allocinfo is the custom type in question
          <xs:element name="Length" type="xs:integer"/>
          <xs:element name="Dir" type="xs:string"/>
          <xs:element name="MsgType" type="xs:string" />
          <!--Figure out how to restrict on set of values-->
          <xs:element name="MsgSubType" type="xs:string"/>
          <xs:element name="MsgTime" type="xs:string"/>
        </xs:sequence>
      </xs:complexType>
    </xs:element>
    <!--End TmsTmh-->

如果有人有定义和利用自己的复杂类型的经验,你能指出我哪里出错吗? 如果我可以提供更多说明或代码,请告诉我。感谢。

1 个答案:

答案 0 :(得分:0)

您需要为complexType定义指定名称,以便从元素定义中引用该名称。此外,如果您的复杂类型具有名称,则它必须显示为<xs:schema>的子元素。看一下这个模式定义:

<xs:schema>
  <xs:element name="Alloc" type="AllocType"/>
  <xs:complexType name="AllocType">
    <xs:sequence>
      <!-- more to follow here -->

请注意元素定义末尾的斜杠。另请注意,复杂类型定义不必位于元素定义之前。

相关问题