我有以下问题:
我正在编写客户端代码以使用 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 由于某种原因不会被反序列化(它们被保留为空)。