SOAP XML仅部分反序列化

时间:2017-06-02 13:46:29

标签: c# xml serialization soap nullpointerexception

我有一段时间从Web服务获取一些简单的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:type =声明,但没有成功。我也尝试用replace方法删除该声明,但没有任何工作。当我最初将此响应转换为C#类时,所有字符串都被定义为元素,而不是属性,我认为这是不正确的。我改变了它们,请告诉我这是否是一个错误(两种方式都无效)。我还使用了响应命名空间,但我不确定这与我当前的错误有什么关系,因为链中的第一个空对象是&#34;返回&#34;。

1 个答案:

答案 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。

唯一的,我不知道如何将班级webServicePO的名称更改为WebServicePO - 收到很多错误。