【问题标题】:Visual Studio 2008 web reference proxy class doesn't decode xml attributeVisual Studio 2008 Web 参考代理类不解码 xml 属性
【发布时间】:2011-01-13 03:12:29
【问题描述】:

我有以下问题:

我正在编写使用 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 由于某种原因没有被反序列化(它们被保留为空)。

【问题讨论】:

    标签: c# .net web-services proxy-classes xml-attribute


    【解决方案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 正确识别出我的测试架构文件中的属性是完全限定的,所以看起来这个问题可能是因为您生成的代码与架构过时(即架构已更新为现在使用限定属性,当您最初从架构生成代码时,这些属性没有被限定)。

    希望对你有帮助!!!

    【讨论】:

    • 你是最棒的!!!顺便说一句,什么是合格的属性名称,与不合格的有什么区别?
    • @Bogi 限定属性包括命名空间前缀(在您的示例中为“conc”)。您提供的示例使用限定属性:&lt;conc:Product conc:ProductID="4C475A0126111982" conc:GroupID="Default" /&gt;。每个属性前面的“conc:”是使属性“合格”的原因。如果这些属性不合格,则此元素将如下所示 &lt;conc:Product ProductID="4C475A0126111982" GroupID="Default" /&gt;
    猜你喜欢
    • 2010-12-09
    • 1970-01-01
    • 2018-11-20
    • 2010-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多