2017-06-02 84 views
0

我有一個時間的惡魔從Web服務中獲得一些簡單的xml以正確反序列化。當我實際反序列化時,沒有任何錯誤,但是最終的對象只能進行深度嵌套 - 一旦到達「返回」標記,它和所有的子元素都是空的。我似乎無法弄清楚爲什麼它在這一點上是失敗的......它似乎與其他類相同。有誰知道什麼是錯的?SOAP XML只能部分反序列化

示例XML響應:

<?xml version="1.0" encoding="UTF-8"?> 
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"> 
<S:Body> 
    <ns2:getPreSubmitInfoResponse xmlns:ns2="http://webservice.integration.someservice.com/"> 
    <return> 
     <errorOccurred>false</errorOccurred> 
     <message>Information returned successfully</message> 
     <response xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns2:webServicePO"> 
      <internalMessage>SUCCESS: Inventory Found</internalMessage> 
      <poNum>56658</poNum> 
      <residence>N</residence> 
      <shipAddress1>TEST</shipAddress1> 
      <shipAddress2 /> 
      <shipCity>TEST</shipCity> 
      <shipMethod>UPS</shipMethod> 
      <shipState>TX</shipState> 
      <shipTo>TEST'S</shipTo> 
      <shipZip>99999</shipZip> 
      <webServicePoDetailList> 
       <color>TEST</color> 
       <errorOccured>false</errorOccured> 
       <inventoryKey>TEST</inventoryKey> 
       <message>Requested Quantity is confirmed and available in warehouse '2' to ship to your destination.</message> 
       <quantity>5</quantity> 
       <size>XS</size> 
       <sizeIndex>1</sizeIndex> 
       <style>TEST</style> 
       <whseNo>2</whseNo> 
      </webServicePoDetailList> 
     </response> 
    </return> 
    </ns2:getPreSubmitInfoResponse> 

C#類:

[XmlRoot(ElementName = "webServicePoDetailList")] 
    public class WebServicePoDetailList 
    { 
     [XmlAttribute(AttributeName = "color")] 
     public string Color { get; set; } 
     [XmlAttribute(AttributeName = "errorOccured")] 
     public string ErrorOccured { get; set; } 
     [XmlAttribute(AttributeName = "inventoryKey")] 
     public string InventoryKey { get; set; } 
     [XmlAttribute(AttributeName = "message")] 
     public string Message { get; set; } 
     [XmlAttribute(AttributeName = "quantity")] 
     public string Quantity { get; set; } 
     [XmlAttribute(AttributeName = "size")] 
     public string Size { get; set; } 
     [XmlAttribute(AttributeName = "sizeIndex")] 
     public string SizeIndex { get; set; } 
     [XmlAttribute(AttributeName = "style")] 
     public string Style { get; set; } 
     [XmlAttribute(AttributeName = "whseNo")] 
     public string WhseNo { get; set; } 
    } 

    [XmlRoot(ElementName = "response")] 
    //[XmlInclude(typeof(webServicePO))] 
    public class Response 
    { 
     [XmlAttribute(AttributeName = "internalMessage")] 
     public string InternalMessage { get; set; } 
     [XmlAttribute(AttributeName = "poNum")] 
     public string PoNum { get; set; } 
     [XmlAttribute(AttributeName = "residence")] 
     public string Residence { get; set; } 
     [XmlAttribute(AttributeName = "shipAddress1")] 
     public string ShipAddress1 { get; set; } 
     [XmlAttribute(AttributeName = "shipAddress2")] 
     public string ShipAddress2 { get; set; } 
     [XmlAttribute(AttributeName = "shipCity")] 
     public string ShipCity { get; set; } 
     [XmlAttribute(AttributeName = "shipMethod")] 
     public string ShipMethod { get; set; } 
     [XmlAttribute(AttributeName = "shipState")] 
     public string ShipState { get; set; } 
     [XmlAttribute(AttributeName = "shipTo")] 
     public string ShipTo { get; set; } 
     [XmlAttribute(AttributeName = "shipZip")] 
     public string ShipZip { get; set; } 
     [XmlElement(ElementName = "webServicePoDetailList")] 
     public WebServicePoDetailList WebServicePoDetailList { get; set; } 
    } 

    //[Serializable()] 
    //[XmlRoot(Namespace = "http://www.w3.org/2001/XMLSchema-instance")] 
    //public class webServicePO : Response { } 

    [XmlRoot(ElementName = "return")] 
    public class Return 
    { 
     [XmlAttribute(AttributeName = "errorOccurred")] 
     public string ErrorOccurred { get; set; } 
     [XmlAttribute(AttributeName = "message")] 
     public string Message { get; set; } 
     [XmlElement(ElementName = "response")] 
     public Response Response { get; set; } 
    } 

    [XmlRoot(ElementName = "getPreSubmitInfoResponse", Namespace = "http://webservice.integration.someservice.com/")] 
    public class GetPreSubmitInfoResponse 
    { 
     [XmlElement(ElementName = "return")] 
     public Return Return { get; set; } 
    } 

    [XmlRoot(ElementName = "Body")] 
    public class Body 
    { 
     [XmlElement(ElementName = "getPreSubmitInfoResponse", Namespace = "http://webservice.integration.someservice.com/")] 
     public GetPreSubmitInfoResponse GetPreSubmitInfoResponse { get; set; } 
    } 

    [XmlRoot(ElementName = "Envelope", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")] 
    public class Envelope 
    { 
     [XmlElement(ElementName = "Body")] 
     public Body Body { get; set; } 
    } 

你會發現,我試圖適應XSI:響應中類型=聲明,但沒有成功。我也嘗試用replace方法刪除該聲明,但沒有任何工作。當我最初將此響應轉換爲C#類時,所有字符串都被定義爲元素,而不是屬性,我認爲這是不正確的。我改變了他們,請讓我知道如果這是一個錯誤(兩種方法都行不通)。我也玩過響應命名空間,但我不確定這與我當前的錯誤有什麼關係,因爲鏈中的第一個空對象是「返回」。

+0

即使對某個問題可能存在的位置有所瞭解也會有所幫助。如果「返回」爲空,那麼這是否意味着問題出現在「返回」類中或者它可能是其中的一個孩子? –

回答

1

不要混淆xml元素和xml屬性。

這組類正確地反序列化了提供的xml。

[XmlRoot(Namespace = "http://schemas.xmlsoap.org/soap/envelope/")] 
public class Envelope 
{ 
    public Body Body { get; set; } 
} 

public class Body 
{ 
    [XmlElement("getPreSubmitInfoResponse", Namespace = "http://webservice.integration.someservice.com/")] 
    public GetPreSubmitInfoResponse GetPreSubmitInfoResponse { get; set; } 
} 

[XmlRoot("getPreSubmitInfoResponse")] 
public class GetPreSubmitInfoResponse 
{ 
    [XmlElement("return", Namespace = "")] 
    public Return Return { get; set; } 
} 

[XmlRoot("return")] 
public class Return 
{ 
    [XmlElement("errorOccurred")] 
    public bool ErrorOccurred { get; set; } 
    [XmlElement("message")] 
    public string Message { get; set; } 
    [XmlElement("response")] 
    public Response Response { get; set; } 
} 

[XmlRoot("response", Namespace = "")] 
[XmlInclude(typeof(webServicePO))] 
public class Response 
{ 
    [XmlElement("internalMessage")] 
    public string InternalMessage { get; set; } 
    [XmlElement("poNum")] 
    public ushort PoNum { get; set; } 
    [XmlElement("residence")] 
    public string Residence { get; set; } 
    [XmlElement("shipAddress1")] 
    public string ShipAddress1 { get; set; } 
    [XmlElement("shipAddress2")] 
    public object ShipAddress2 { get; set; } 
    [XmlElement("shipCity")] 
    public string ShipCity { get; set; } 
    [XmlElement("shipMethod")] 
    public string ShipMethod { get; set; } 
    [XmlElement("shipState")] 
    public string ShipState { get; set; } 
    [XmlElement("shipTo")] 
    public string ShipTo { get; set; } 
    [XmlElement("shipZip")] 
    public uint ShipZip { get; set; } 
    [XmlElement("webServicePoDetailList")] 
    public WebServicePoDetailList WebServicePoDetailList { get; set; } 
} 

[XmlRoot("webServicePoDetailList", Namespace = "")] 
public class WebServicePoDetailList 
{ 
    [XmlElement("color")] 
    public string Color { get; set; } 
    [XmlElement("errorOccured")] 
    public bool ErrorOccured { get; set; } 
    [XmlElement("inventoryKey")] 
    public string InventoryKey { get; set; } 
    [XmlElement("message")] 
    public string Message { get; set; } 
    [XmlElement("quantity")] 
    public byte Quantity { get; set; } 
    [XmlElement("size")] 
    public string Size { get; set; } 
    [XmlElement("sizeIndex")] 
    public byte SizeIndex { get; set; } 
    [XmlElement("style")] 
    public string Style { get; set; } 
    [XmlElement("whseNo")] 
    public byte WhseNo { get; set; } 
} 

[XmlRoot("webServicePO", Namespace = "http://webservice.integration.someservice.com/")] 
public class webServicePO : Response { } 

用法:

Envelope envelope; 
var serializer = new XmlSerializer(typeof(Envelope)); 

using (var fs = new FileStream("test.xml", FileMode.Open)) 
    envelope = (Envelope)serializer.Deserialize(fs); 

using (var fs = new FileStream("test2.xml", FileMode.Create)) 
    serializer.Serialize(fs, envelope); 

系列化後會獲得相同的XML。


唯一的,我不知道如何更改名稱的類webServicePOWebServicePO - 獲得了大量的錯誤。

+0

謝謝!我會花一些時間來分析差異。 –