svcutil 没有为 <element type="..." 生成 class

svcutil not generating class for <element type="..."

我有一个模式,我正在尝试将其编译到数据协定中。我发现如果元素定义为 <xs:element name="DogRequest" type="Dog"></xs:element> 则不会为 DogRequest 生成 class。我想使用 svcutil,因为我要生成多个命名空间,而 xsd.exe 只允许一个。另外,我有几个使用相同类型的元素,xsd.exe 只生成其中一个。有谁知道是否有办法为此架构生成 classes?

我正在使用采用 XML 负载的通用 Web 服务。我希望仍然使用 WCF 来构建消息。

架构

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="XMLSchema1"
    targetNamespace="http://tempuri.org/XMLSchema1.xsd"
    elementFormDefault="qualified"
    xmlns="http://tempuri.org/XMLSchema1.xsd"
    xmlns:mstns="http://tempuri.org/XMLSchema1.xsd"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
  <xs:complexType name="Dog">
    <xs:sequence>
      <xs:element name="Name" type="xs:string"></xs:element>
    </xs:sequence>
  </xs:complexType>

  <xs:element name="DogRequest" type="Dog"></xs:element>
</xs:schema>

编译为 svcutil /dconly XMLSchema1.xsd

这将为 Dog 生成 1 class,但不会为 DogRequest 生成任何内容。

xsd.exe 将为带有 DogRequest

的 Dog 生成 1 class

svcutil 输出

[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")]
[System.Runtime.Serialization.DataContractAttribute(Name="Dog", Namespace="http://tempuri.org/XMLSchema1.xsd")]
public partial class Dog : object, System.Runtime.Serialization.IExtensibleDataObject
{

    private System.Runtime.Serialization.ExtensionDataObject extensionDataField;

    private string NameField;

    public System.Runtime.Serialization.ExtensionDataObject ExtensionData
    {
        get
        {
            return this.extensionDataField;
        }
        set
        {
            this.extensionDataField = value;
        }
    }

    [System.Runtime.Serialization.DataMemberAttribute(IsRequired=true, EmitDefaultValue=false)]
    public string Name
    {
        get
        {
            return this.NameField;
        }
        set
        {
            this.NameField = value;
        }
    }
}

XSD输出

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.17929")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://tempuri.org/XMLSchema1.xsd")]
[System.Xml.Serialization.XmlRootAttribute("DogRequest", Namespace="http://tempuri.org/XMLSchema1.xsd", IsNullable=false)]
public partial class Dog {

    private string nameField;

    /// <remarks/>
    public string Name {
        get {
            return this.nameField;
        }
        set {
            this.nameField = value;
        }
    }
}

您对 WSDL 的理解有误。 DogRequest 不是类型,这就是为什么没有创建 class 的原因。模式 <element> 定义了一个 xml 元素,可以在 XSD/WSDL 的其他部分使用它来引用 Dog 复杂类型。

例如,您的 WSDL 可能有一段内容如下:

<message name="DogMessage">
    <part name="parameter" element="tns:DogRequest"/>
</message>

其中 tns 是您的目标命名空间。有关详细信息,请参阅 Understanding WSDL 的类型部分。

当XSD工具将XmlRootAttribute("DogRequest")添加到狗class时,定义<DogRequest>元素可能是[=37]的文档根=] 消息应该是 serialized/deserialized to/from Dog class.

来自 XmlRootAttribute:

The XmlRootAttribute allows you to control how the XmlSerializer generates the root element by setting certain properties. For example, specify the name of the generated XML element by setting the ElementName property.