【问题标题】:XML creates element name from XSD ComplexType, not from Element nameXML 从 XSD ComplexType 创建元素名称,而不是从元素名称
【发布时间】:2019-10-12 19:33:38
【问题描述】:

我有一个使用 SVCUTIL.exe 生成到 C# 代码中的 XSD 架构。然后我用数据填充生成的代码并将其作为响应返回到 WCF 服务中。

在生成的 xml 响应中,在我的一个元素中,元素名称是复杂类型的名称,而不是 XSD 中的元素名称。 示例:

  <xs:complexType name="ReadSchoolsResponseType">
    <xs:sequence>
      <xs:element name="Schools" type="SchoolsType"/>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="SchoolsType">
    <xs:sequence>
      <xs:element name="School" type="SchoolType" maxOccurs="unbounded"/>
    </xs:sequence>
  </xs:complexType>

生成的 XML 是:


<ReadSchoolsResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Schools>
    <SchoolType> </SchoolType>
  </Schools>
</ReadSchoolsResponse

为什么 School 元素呈现为 SchoolType?我有很多以相同方式创建的元素,并且可以正确序列化。 谢谢。

【问题讨论】:

    标签: xml xsd


    【解决方案1】:

    所以我想出了解决方案: 删除 SchoolsType 复杂类型名称并将元素详细信息移入。

      <xs:complexType name="SchoolsType">
        <xs:sequence>
          <xs:element name="School" maxOccurs="unbounded"/>
            <xs:complexType>
              <xs:sequence>
                <xs:element name="asdf" type="xs:int"/>
                <xs:element name="qwer" type="xs:dateTime"/>
                <xs:element name="yxcv" type="xs:string"/>
                <xs:element name="tzui" type="xs:string"/>
              </xs:sequence>
            </xs:complexType>
          </xs:element>
        </xs:sequence>
      </xs:complexType>
    

    但是我仍然不知道为什么有问题的代码不起作用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-06
      相关资源
      最近更新 更多