使用 svcutil 从 xsd 生成 DataContracts 时出现问题

Problem with generating DataContracts from xsd with svcutil

我正在尝试根据我的 xsd 文件生成带有 类 注释的 DataContract。这些 类 应用作我的 Soap Web 服务的交换数据类型。

在 cmd.exe 我试过 运行 命令:

svcutil /dconly loginSoap.xsd /language:C#

但失败并出现以下错误:

Error: Type 'loginRequest' in namespace 'http://www.megatravel.xyz/XMLSchema/XMLSchemaSoap/Login' cannot be imported. The root particle must be a sequence. Either change the schema so that the types can map to data contract types or use ImportXmlType 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 the XmlSerializerFormatAttribute attribute on your service contract. Alternatively, consider using the /importXmlTypes option to import these types as XML types to use with DataContractFormatAttribute attribute on your service contract.

我的 xsd 文件是:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns="http://www.megatravel.xyz/XMLSchema/XMLSchemaSoap/Login"
           targetNamespace="http://www.megatravel.xyz/XMLSchema/XMLSchemaSoap/Login"
           elementFormDefault="qualified">
    <xs:element name="loginRequest">
        <xs:complexType>
            <xs:all>
                <xs:element name="username" type="xs:string"/>
                <xs:element name="password" type="xs:string"/>
            </xs:all>
        </xs:complexType>
    </xs:element>
    <xs:element name="loginResponse">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="message" type="xs:string"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

错误为您指明了正确的方向。

只需将 <xs:all> 替换为 <xs:sequence>

或将 XSD.exe 与 /classes

一起使用