【问题标题】:Deserializing SOAP response into generated classes C#将 SOAP 响应反序列化为生成的类 C#
【发布时间】:2016-10-27 12:03:29
【问题描述】:

我们正在与第三方集成,并且无法反序列化我们从请求中获得的响应。从提琴手,我可以看到响应:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body>
  <ns2:updateItemsResponse xmlns:ns2="http://ThirdParty/dmn/items/1.1">
     <fileId>bd6e7d83-8bfu-4573-fe32-c67c04355dd0</fileId>
     <fileName>12345.MBG_Items.002.2016-10-26-16-23-35.xml</fileName>
  </ns2:updateItemsResponse>

但我们无法将其正确反序列化为 updateItemsResponse 对象。对象已创建,但 fileId 和 fileName 均为空。 我们为此请求和响应生成了类,下面是生成的类:

[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
[System.ServiceModel.MessageContractAttribute(WrapperName = "updateItemsResponse", WrapperNamespace = "http://ThirdParty/dmn/items/1.1", IsWrapped = true)]
public partial class updateItemsResponse
{
    [System.ServiceModel.MessageBodyMemberAttribute(Namespace = "http://ThirdParty/dmn/items/1.1", Order = 0)]
    public string fileName;

    [System.ServiceModel.MessageBodyMemberAttribute(Namespace = "http://ThirdParty/dmn/items/1.1", Order = 1)]
    public string fileId;

    public updateItemsResponse()
    {
    }

    public updateItemsResponse(string fileId, string fileName)
    {
        this.fileId= fileId;
        this.fileName= fileName;
    }
}

我们知道 Web 服务是用 Java 编写的,并且我们在生成类文件时遇到了一些问题 - 我们提供的 wsdl 需要一些复杂的操作才能使用 svcutil.exe 创建类文件。所以我认为我需要在生成的文件中进行一些调整。

我已经对此进行了一些搜索,我猜这里的问题是命名空间,但我无法让它工作。我尝试过更改命名空间、删除命名空间、更改 fileId 和 fileName 的顺序 - 但这些都没有任何区别。

任何人都能看到我可能遗漏的任何明显的东西吗?快把我逼疯了!!!

编辑:也尝试过将 fileId 和 fileName 更改为 XmlElement,但仍然得到空响应。

【问题讨论】:

  • fileId 和 fileName 是否需要 Namespace 限定属性?他们似乎没有在提琴手输出中使用命名空间
  • 我已经尝试删除命名空间属性。但没有快乐!
  • 尝试添加此属性:[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
  • 谢谢@IanofOz,刚才试过了,但还是不开心。
  • 对不起,我不能再添加任何东西了......

标签: java c# web-services soap deserialization


【解决方案1】:

尤里卡!!!

所以这是错误的属性 - 在 fileId 和 fileName 上。只需要将它们更改为

[System.ServiceModel.MessageBodyMemberAttribute(Name = "fileId", Namespace = "", Order = 0)]
    public string fileId;

    [System.ServiceModel.MessageBodyMemberAttribute(Name = "fileName", Namespace = "", Order = 1)]
    public string fileName;

我现在可以反序列化响应。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-05
    • 2015-01-19
    • 1970-01-01
    • 1970-01-01
    • 2012-03-04
    相关资源
    最近更新 更多