开发者

Visual Studio 2008 web reference proxy class doesn't decode xml attribute

开发者 https://www.devze.com 2023-02-04 14:19 出处:网络
I have a following issue: I am writing client code for consumation of a web service. Here is the answer from the web service:

I have a following issue:

I am writing client code for consumation of a web service. Here is the answer from the web service:

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>

Here is the definition from the .xsd and .wsdl files which works with this:

  <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>

I used Add Web Reference to add the reference. I used .NET 2.0 styled web services.

Here are the generated proxy classes:

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "2.0.50727.4927")]
[System.SerializableAttribute()]
[System.Diagnostics.开发者_StackOverflow社区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;
        }
    }
}

The problem is, when the web service gets deserialized, ProductID and GroupID don't get deserialized (they are left to null) for some reason.


I was close the first time, but this is actually a bit different problem than I originally thought.

The problem is that the attributes in the web service response are qualified with the namespace prefix, but the generated code does identify that the attributes are qualified. Update the following code to add the System.Xml.Schema.XmlSchemaForm.Qualified paramter in the attributes and it should start populating those objects when you get the response.

    /// <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 correctly recognized that the attributes in my test schema file were fully qualified, so it seems like this problem might have occurred because your generated code is out-of-date with the schema (i.e. the schema has been updated to now use qualified attributes where the attributes weren't qualified when you originally generated the code from the schema).

Hope that helps!!!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号