【问题标题】:.NET webservice client null properties in result结果中的 .NET Web 服务客户端 null 属性
【发布时间】:2012-02-25 03:08:01
【问题描述】:

在 C# 控制台应用程序中看似成功地使用 Web 服务后,检查返回的对象显示属性全部为空!

在大量寻找答案之后,我将问题归结为 SOAP XML 中的命名空间问题——使用 fiddler,我稍微修改过的 SOAP 响应如下所示:

  <?xml version="1.0" encoding="utf-8"?>
  <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <soapenv:Body>
      <MyResponse xmlns="http://foo.com">
        <FIRST_NM xmlns="">Michael</FIRST_NM>
      </MyResponse>
    </soapenv:Body>
  </soapenv:Envelope>

我添加的唯一部分是:

...FIRST_NM xmlns="">Michael...(粗体部分)使用提琴手。

一旦我在调试会话期间作为中间人执行此操作 - 瞧! “Michael”的 FIRST_NM 值第一次在我的客户端应用程序中显示给我。我决定尝试这个,因为我注意到从我的客户端发出的请求具有这样的属性,而响应没有。

好的,诊断完成。我正在转向让它发挥作用的步骤。假设我无法控制服务器端,我尝试打开 Reference.cs 文件并进行一些更改,例如更改:

[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, Order=3)]
 public string FIRST_NM {
   get {
     return this.fIRST_NMField;
   }
   set {
     this.fIRST_NMField = value;
     this.RaisePropertyChanged("FIRST_NM");
   }
 }

到这里:

[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, Namespace="http://foo.com", Order=3)]
public string FIRST_NM {
...

我将 Namespace="http://foo.com", 添加到 XmlElementAttribute。我的想法是,如果没有 xmlns="",命名空间“http://foo.com”是从父“MyResponse”标签继承的,我会得到一个匹配项。

然而,这并没有奏效。任何你们都可以指出我的方向将不胜感激。

【问题讨论】:

    标签: c# web-services soap asmx


    【解决方案1】:

    我能够通过从 .NET 生成的代码中完全删除以下行来解决该问题:

    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, Order=3)]
    

    我想由于命名空间是从父标记继承的,因此它实际上是“合格”而不是“不合格”。删除它会导致默认的 Form 行为,None(它遵循文档)。

    感谢社区——我主要是根据我在相关 SO Questions 中找到的建议来解决这个问题的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-04
      • 2022-08-12
      相关资源
      最近更新 更多