0

我有以下问题:

我正在编写客户端代码以使用 Web 服务。以下是来自网络服务的答案:

HTTP/1.0 200 OK
Content-Type: text/xml; charset=utf-8
Connection: close

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <conc:GetProductsListResponse xmlns:conc="http://tempuri.org/XMLSchema.xsd">
      <conc:Result>
        <conc:Products>
           <conc:Product conc:ProductID="4C475A0126111982" conc:GroupID="Default" />
        </conc:Products>
      </conc:Result>
    </conc:GetProductsListResponse>
  </soap:Body>
 </soap:Envelope>

以下是 .xsd 和 .wsdl 文件的定义,适用于此:

  <xs:element name="GetProductsListResponse">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Result">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="Products" type="ProductsType" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

  <xs:complexType name="ProductsType">
    <xs:choice>
      <xs:element name="Product" maxOccurs="unbounded">
        <xs:complexType>
          <xs:attribute name="ProductID" type="xs:string"/>
          <xs:attribute name="GroupID" type="xs:string"/>
        </xs:complexType>
      </xs:element>
      <xs:element name="Error">
        <xs:complexType>
          <xs:attribute name="ErrorID" type="xs:string"/>
        </xs:complexType>
      </xs:element>
    </xs:choice>
  </xs:complexType>

我使用 Add Web Reference 添加引用。我使用了 .NET 2.0 风格的 Web 服务。

以下是生成的代理类:

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "2.0.50727.4927")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://tempuri.org/XMLSchema.xsd")]
public partial class GetProductsListResponse {

    private GetProductsListResponseResult resultField;

    /// <remarks/>
    public GetProductsListResponseResult Result {
        get {
            return this.resultField;
        }
        set {
            this.resultField = value;
        }
    }
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "2.0.50727.4927")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://tempuri.org/XMLSchema.xsd")]
public partial class GetProductsListResponseResult {

    private ProductsType productsField;

    /// <remarks/>
    public ProductsType Products {
        get {
            return this.productsField;
        }
        set {
            this.productsField = value;
        }
    }
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "2.0.50727.4927")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://tempuri.org/XMLSchema.xsd")]
public partial class ProductsType {

    private ProductsTypeProduct[] itemsField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("Product")]
    public ProductsTypeProduct[] Items {
        get {
            return this.itemsField;
        }
        set {
            this.itemsField = value;
        }
    }
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "2.0.50727.4927")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://tempuri.org/XMLSchema.xsd")]
public partial class ProductsTypeProduct {

    private string productIDField;

    private string groupIDField;

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string ProductID {
        get {
            return this.productIDField;
        }
        set {
            this.productIDField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string GroupID {
        get {
            return this.groupIDField;
        }
        set {
            this.groupIDField = value;
        }
    }
}

问题是,当 Web 服务被反序列化时,ProductID 和 GroupID 由于某种原因不会被反序列化(它们被保留为空)。

4

1 回答 1

1

我第一次很接近,但这实际上与我最初想象的有点不同。

问题是 Web 服务响应中的属性是用命名空间前缀限定的,但生成的代码确实识别出这些属性是限定的。更新以下代码以System.Xml.Schema.XmlSchemaForm.Qualified在属性中添加参数,它应该在您收到响应时开始填充这些对象。

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute(Form=System.Xml.Schema.XmlSchemaForm.Qualified")]
    public string ProductID {
        get {
            return this.productIDField;
        }
        set {
            this.productIDField = value;
        }
    }
    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute(Form=System.Xml.Schema.XmlSchemaForm.Qualified")]
    public string GroupID {
        get {
            return this.groupIDField;
        }
        set {
            this.groupIDField = value;
        }
    }

XSD.exe 正确识别出我的测试架构文件中的属性是完全合格的,所以看起来这个问题可能是因为您生成的代码与架构过时(即架构已更新为现在使用合格的当您最初从架构生成代码时属性未限定的属性)。

希望有帮助!!!

于 2011-01-12T15:57:44.210 回答