反序列化SOAP响应中的对象

时间:2014-11-21 16:57:33

标签: c# .net web-services serialization soap

我试图反序列化SOAP响应 肥皂反应是这样的:

<soapenv:Body>
      <mp0:MP0123_ConnectorRequest_001_Result>
         <!--You have a CHOICE of the next 2 items at this level-->
         <mp0:result success="true">
            <!--1 or more repetitions:-->
            <mp0:actionResult type="?" aid="?">
               <mp0:success>?</mp0:success>
               <!--Optional:-->
               <mp0:errorMessage>?</mp0:errorMessage>
               <!--Zero or more repetitions:-->
               <mp0:dataResult>
                  <!--1 or more repetitions:-->
                  <mp0:datafield>
                     <bas:name>?</bas:name>
                     <bas:value null="false" encoding="none">?</bas:value>
                  </mp0:datafield>
               </mp0:dataResult>
            </mp0:actionResult>
         </mp0:result>
         <mp0:result-in-file>
            <bas:ticket>?</bas:ticket>
            <bas:members>
               <!--You have a CHOICE of the next 2 items at this level-->
               <bas:sequence min="?" max="?"/>
               <bas:lables>
                  <!--1 or more repetitions:-->
                  <bas:fr-label aid="?">?</bas:fr-label>
               </bas:lables>
            </bas:members>
         </mp0:result-in-file>
      </mp0:MP0123_ConnectorRequest_001_Result>
   </soapenv:Body>

从WSDL生成的类是这样的:

/// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.34234")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://schemas.datastream.net/MP_results/MP0123_001")]
    public partial class MP0123_ConnectorRequest_001_Result {

        private object itemField;

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("result", typeof(MP0123_ConnectorRequest_001_ResultResult))]
        [System.Xml.Serialization.XmlElementAttribute("result-in-file", typeof(fileResult))]
        public object Item {
            get {
                return this.itemField;
            }
            set {
                this.itemField = value;
            }
        }
    }

   /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.34234")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://schemas.datastream.net/MP_results/MP0123_001")]

public partial class MP0123_ConnectorRequest_001_ResultResult {

private MP0123_ConnectorRequest_001_ResultResultActionResult[] actionResultField;

private bool successField;

public MP0123_ConnectorRequest_001_ResultResult() {
    this.successField = true;
}

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("actionResult")]
public MP0123_ConnectorRequest_001_ResultResultActionResult[] actionResult {
    get {
        return this.actionResultField;
    }
    set {
        this.actionResultField = value;
    }
}

/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
[System.ComponentModel.DefaultValueAttribute(true)]
public bool success {
    get {
        return this.successField;
    }
    set {
        this.successField = value;
    }
}

}

现在我通过以下代码获得响应,但是我唯一拥有的是Response.Item,它是一个我无法反序列化它的对象。

var response = connector.ConnectorRequestOp(Request);
var serializer = new XmlSerializer(typeof(ConnectorRequest_001_ResultResult),"http://schemas.datastream.net/MP_results/MP0123_001");
var  responseREsult = (MP0123_ConnectorRequest_001_ResultResult)serializer.Deserialize(new MemoryStream(response.Item));

我知道在这种情况下我不能使用MemoryStream,因此我搜索了很多并在此处阅读了类似的帖子,但我找到的唯一可行方法是使用路径并使用FileStream但是我没有回应路径(我认为)。那么有什么方法可以反序列化一个对象吗?

我不知道在序列化/反序列化肥皂反应中是否遗漏了一些重要概念,因为这是我第一次处理这个问题。

由于

1 个答案:

答案 0 :(得分:0)

您应该能够按如下方式访问结果:

var response = connector.ConnectorRequestOp(Request);

if(response != null && response.Item != null)
{
    if(response.Item Is MP0123_ConnectorRequest_001_ResultResult)
    {
        var responseResult = response.Item As MP0123_ConnectorRequest_001_ResultResult;
        // ...
    }
    else
    {
        var otherResult = response.Item As fileResult
        // ...
    }
}