2012-10-12 44 views
4

我正在使用Windows應用商店應用程序。反序列化SOAP消息的詳細信息

我有以下的手動創建的故障類:

[XmlRoot(Namespace = "http://schemas.xmlsoap.org/soap/envelope/", ElementName = "Fault")] 
public partial class SoapFault 
{ 
    [XmlElement(Form = XmlSchemaForm.Unqualified, ElementName = "faultcode")] 
    public String FaultCode { get; set; } 

    [XmlElement(Form = XmlSchemaForm.Unqualified, ElementName = "faultstring")] 
    public String FaultDescription { get; set; } 

    [XmlElement(Form = XmlSchemaForm.Unqualified, ElementName = "detail")] 
    public InnerException[] Detail { get; set; } 
} 

    [XmlType(Namespace = "http://my.namespace.com", TypeName = "InnerException")] 
    public partial class InnerException 
    { 
     [XmlElement(Form = XmlSchemaForm.Unqualified, ElementName = "message")] 
     public String Message { get; set; } 
    } 

這是我試圖讀取服務器的回覆:

<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"><env:Body><env:Fault><faultcode>env:Server</faultcode><faultstring>internal error</faultstring><detail><ns2:InnerException xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://ns1.my.namespace.com" xmlns:ns2="http://my.namespace.com" xmlns:ns3="http://ns3.my.namespace.com"><message>internal error</message></ns2:InnerException ></detail></env:Fault></env:Body></env:Envelope> 

我想讀它是作爲的方式如下:

using (XmlReader reader = XmlReader.Create(await response.Content.ReadAsStreamAsync())) 
{ 
    string SoapNamespace = "http://schemas.xmlsoap.org/soap/envelope/"; 
    try 
    { 
     var serializer = new XmlSerializer(typeof(SoapFault)); 

     reader.ReadStartElement("Envelope", SoapNamespace); 
     reader.ReadStartElement("Body", SoapNamespace); 

     var fault = serializer.Deserialize(reader) as SoapFault; 

     reader.ReadEndElement(); 
     reader.ReadEndElement(); 
    } 
    catch(Exception ex) 
    { 
     throw new Exception("Exception was thrown:" + ex.Message); 
    } 
} 

我已經嘗試添加名稱空間,更改XmlElement屬性,但我總是結束將SoapFault中的Detail屬性設置爲NULL。當我將類型更改爲對象時,我至少得到一個包含數據的XmlNode集。

我必須改變這段代碼才能獲得在序列化中實例化的正確類。

請注意:我不幸被迫手動創建調用,並且不能使用自動生成的代碼。

回答

6

深受How to Deserialize XML document的啓發,我認爲這應該可以做到。

我生成的類,如下所示:

  1. 保存XML服務器回覆到磁盤(C:\ TEMP \ reply.xml)
  2. XSD C:\ TEMP \ reply.xml/O:」 c:\ temp「
  3. xsd c:\ temp \ reply.xsd reply_app1.xsd/classes/o:」c:\ temp「
  4. 將reply_app1.cs添加到您的項目中。

這導致:

//------------------------------------------------------------------------------ 
// <auto-generated> 
//  This code was generated by a tool. 
//  Runtime Version:4.0.30319.269 
// 
//  Changes to this file may cause incorrect behavior and will be lost if 
//  the code is regenerated. 
// </auto-generated> 
//------------------------------------------------------------------------------ 

using System.Xml.Serialization; 

// 
// This source code was auto-generated by xsd, Version=4.0.30319.1. 
// 

/// <remarks/> 
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")] 
[System.SerializableAttribute()] 
[System.Diagnostics.DebuggerStepThroughAttribute()] 
[System.ComponentModel.DesignerCategoryAttribute("code")] 
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://schemas.xmlsoap.org/soap/envelope/")] 
[System.Xml.Serialization.XmlRootAttribute(Namespace="http://schemas.xmlsoap.org/soap/envelope/", IsNullable=false)] 
public partial class Envelope { 

    private EnvelopeBody[] itemsField; 

    /// <remarks/> 
    [System.Xml.Serialization.XmlElementAttribute("Body")] 
    public EnvelopeBody[] Items { 
     get { 
      return this.itemsField; 
     } 
     set { 
      this.itemsField = value; 
     } 
    } 
} 

/// <remarks/> 
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")] 
[System.SerializableAttribute()] 
[System.Diagnostics.DebuggerStepThroughAttribute()] 
[System.ComponentModel.DesignerCategoryAttribute("code")] 
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://schemas.xmlsoap.org/soap/envelope/")] 
public partial class EnvelopeBody { 

    private EnvelopeBodyFault[] faultField; 

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

/// <remarks/> 
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")] 
[System.SerializableAttribute()] 
[System.Diagnostics.DebuggerStepThroughAttribute()] 
[System.ComponentModel.DesignerCategoryAttribute("code")] 
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://schemas.xmlsoap.org/soap/envelope/")] 
public partial class EnvelopeBodyFault { 

    private string faultcodeField; 

    private string faultstringField; 

    private EnvelopeBodyFaultDetail detailField; 

    /// <remarks/> 
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] 
    public string faultcode { 
     get { 
      return this.faultcodeField; 
     } 
     set { 
      this.faultcodeField = value; 
     } 
    } 

    /// <remarks/> 
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] 
    public string faultstring { 
     get { 
      return this.faultstringField; 
     } 
     set { 
      this.faultstringField = value; 
     } 
    } 

    /// <remarks/> 
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] 
    public EnvelopeBodyFaultDetail detail { 
     get { 
      return this.detailField; 
     } 
     set { 
      this.detailField = value; 
     } 
    } 
} 

/// <remarks/> 
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")] 
[System.SerializableAttribute()] 
[System.Diagnostics.DebuggerStepThroughAttribute()] 
[System.ComponentModel.DesignerCategoryAttribute("code")] 
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://schemas.xmlsoap.org/soap/envelope/")] 
public partial class EnvelopeBodyFaultDetail { 

    private InnerException innerExceptionField; 

    /// <remarks/> 
    [System.Xml.Serialization.XmlElementAttribute(Namespace="http://my.namespace.com")] 
    public InnerException InnerException { 
     get { 
      return this.innerExceptionField; 
     } 
     set { 
      this.innerExceptionField = value; 
     } 
    } 
} 

/// <remarks/> 
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")] 
[System.SerializableAttribute()] 
[System.Diagnostics.DebuggerStepThroughAttribute()] 
[System.ComponentModel.DesignerCategoryAttribute("code")] 
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://my.namespace.com")] 
[System.Xml.Serialization.XmlRootAttribute(Namespace="http://my.namespace.com", IsNullable=false)] 
public partial class InnerException { 

    private string messageField; 

    /// <remarks/> 
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] 
    public string message { 
     get { 
      return this.messageField; 
     } 
     set { 
      this.messageField = value; 
     } 
    } 
} 

是的,產生它的汽車,但我想這將是創建一個類,你就可以反序列化的最簡單方法。你可以這樣做:

XmlDocument document = new XmlDocument(); 
document.Load(Server.MapPath("~/Data/reply.xml")); 

System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(Envelope)); 
Envelope envelope = (Envelope)serializer.Deserialize(new StringReader(document.OuterXml)); 

其正確拿起Detail財產。在你的情況,你會需要使用XmlReader像你之前不是做我new StringReader(document.OuterXml),並且在使用塊包起來,等

請記住,一些反序列化對象的屬性名稱不會與你的SoapFault類(例如現在稱爲Envelope)中的內容完全匹配,並且其中一些屬性表示爲數組而非單個對象,但您可以調整Xsd以適應需要。

+1

對於上述步驟#3,如果步驟#2生成的文件超過2個,則可能需要將它們全部列爲xsd的參數,即。 ** xsd ** * reply.xsd * * reply_app1.xsd * * reply_app2.xsd * ... etc – mungflesh

+0

你是一個拯救生命的人 – Yiping