2011-01-12 61 views
0

我有以下问题:Visual Studio 2008的Web引用代理类不解码xml属性

我正在编写Web服务消费的客户端代码。下面是从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> 

我使用添加Web引用添加引用。我使用.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服务被反序列化,产品ID和群ID没有被反序列化(它们留为null)出于某种原因。

回答

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正确地认识到,在我的测试架构文件的属性是完全合格的,所以它看起来像(可能出现这个问题,因为你生成的代码外的日期与该模式即模式已经更新为现在在最初从模式生成代码时属性未被限定的合格属性)。

希望有帮助!

+0

你是最棒的!顺便说一下,什么是合格的属性名称和不合格的区别是什么? – Bogi 2011-01-13 08:12:12

相关问题