使用 WCF 数据协定序列化程序对嵌套对象树进行反序列化

本文关键字:对象 嵌套 反序列化 程序 数据 WCF 序列化 使用 | 更新日期: 2023-09-27 18:33:46

我有以下 SOAP XML 响应

<startProcessResponseDDWEBCall xmlns="http://eclipse.org/stardust/models/generated/OmniLinkServices">
     <ProcessInstanceOid>13430</ProcessInstanceOid>
     <Return>
        <DDWEBCallResponseData>
           <DDWEBCallOutput xmlns="http://www.infinity.com/bpm/model/OmniLinkServices/DDWEBCallOutput">
              <CommonResponse xmlns=""/>
              <reportContent xmlns=""><![CDATA[<HTML><BODY><P>The OmniPlus Host Server process DDWEB had the following error:  </P><P><TEXT="FF000">E23 TX00087 Textfile not found: TestScript @ 000003'''                                                                                                    </TEXT></P><P>processing terminated</P></BODY></HTML>]]></reportContent>
           </DDWEBCallOutput>
        </DDWEBCallResponseData>
     </Return>
  </startProcessResponseDDWEBCall>

这是 WCF Web 服务调用响应的 SOAP 正文。 我有以下对象层次结构来表示响应

[DataContract(Namespace="http://www.infinity.com/bpm/model/OmniLinkServices/DDWEBCallOutput")]
public class OmniLinkExecuteScriptOutput
{   
    public string CommonResponse { get; set; }
    [DataMember(Name = "reportContent")]
    public string ReportContent { get; set; }
}
[DataContract(Namespace = "http://eclipse.org/stardust/models/generated/OmniLinkServices")]
public class OmniLinkExecuteScriptResponseData
{    
    [DataMember(Name="DDWEBCallOutput")]
    public OmniLinkExecuteScriptOutput Output { get; set; }
}
[DataContract(Namespace = "http://eclipse.org/stardust/models/generated/OmniLinkServices")]    
public class OmniLinkExecuteScriptReturn
{ 
    [DataMember(Name="DDWEBCallResponseData")]
    public OmniLinkExecuteScriptResponseData ReponseData { get; set; }
}
[MessageContract(WrapperName = "startProcessResponseDDWEBCall", WrapperNamespace = "http://eclipse.org/stardust/models/generated/OmniLinkServices", IsWrapped = true)]
public class OmniLinkExecuteScriptResponse
{
    [MessageBodyMember(Name = "ProcessInstanceOid", Namespace = "http://eclipse.org/stardust/models/generated/OmniLinkServices")]
    public string ProcessInstanceOid { get; set; }
    [MessageBodyMember(Name = "Return", Namespace = "http://eclipse.org/stardust/models/generated/OmniLinkServices")]
    public OmniLinkExecuteScriptReturn Return { get; set; }
}

对象 OmniLinkExecuteScriptResponse 是方法调用的返回类型。 除了最内部的对象,即表示 DDWEBCallOutput 节点的对象之外,所有内容都进行了很好的反序列化。 我在 WCF 管道中没有看到任何错误,并且 OmniLinkExecuteScriptResponseData 对象上的 Output 属性的值始终为空。

谁能说出我做错了什么?

使用 WCF 数据协定序列化程序对嵌套对象树进行反序列化

您的问题是您在类层次结构中的错误级别应用了[DataContract(Namespace = "...")]属性。 此属性控制将某个类型的实例的所有数据成员序列化到的命名空间,此外,如果实例正在序列化,则还可以控制根元素命名空间。

因此,您需要执行以下操作:

// Its members are in the empty namespace
[DataContract(Namespace = "")] 
public class OmniLinkExecuteScriptOutput
{
    [DataMember]
    public string CommonResponse { get; set; }
    [DataMember(Name="reportContent")]
    public string ReportContent { get; set; }
}
// Its members are in the DDWEBCallOutput namespace
[DataContract(Namespace = "http://www.infinity.com/bpm/model/OmniLinkServices/DDWEBCallOutput")] 
public class OmniLinkExecuteScriptResponseData
{
    [DataMember(Name = "DDWEBCallOutput")]
    public OmniLinkExecuteScriptOutput Output { get; set; }
}
// Its members are in the OmniLinkServices namespace
[DataContract(Namespace = "http://eclipse.org/stardust/models/generated/OmniLinkServices")] 
public class OmniLinkExecuteScriptReturn
{
    [DataMember(Name = "DDWEBCallResponseData")]
    public OmniLinkExecuteScriptResponseData ReponseData { get; set; }
}

OmniLinkExecuteScriptResponse可以保持不变。