在反序列化我的XML时,我收到XML无效的错误

时间:2017-08-10 15:29:45

标签: c# soap xml-deserialization

我有以下SOAP响应:

hidden

我想将其反序列化为以下类:

<?xml version="1.0"?>
 <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <soap:Body>
     <insertResponse xmlns="order">
       <out xmlns="order">
           <actionType xmlns="http://serverurl.com">insert</actionType>
           <orderNumber xmlns="http://serverurl.com">54556100766</orderNumber>   
          <errorMsg xmlns="http://serverurl.com">
             <ns1:Error xmlns:ns1="http://exception.serverurl.com">
                <code xmlns="http://exception.serverurl.com">12345</code>
                <message xmlns="http://exception.serverurl.com">Unable to acquire ID. Record does not exist.</message>
              </ns1:Error>
           </errorMsg>
           <orderID xmlns="http://serverurl.com">0000005555</orderID>
           <region xmlns="http://serverurl.com">Region11</region>
           <successMsg xmlns="http://serverurl.com" xsi:nil="true"/>
       </out>
     </insertResponse>
   </soap:Body>
</soap:Envelope>

我有一种尝试这样做的方法:

public class insertResponse
{
    public string successMsg { get; set; }
    public errorMsg error { get; set ;}
}

public class errorMsg
{
    List<Error> errorList { get; set; }
}

public class Error
{
    public string code { get; set; }
    public string message { get; set; }
}

执行它时,我在private T DeserializeSoapResponse<T>(string soapResponse) { System.Xml.XmlDocument xmlDocument = new System.Xml.XmlDocument(); xmlDocument.LoadXml(soapResponse); var soapBody = xmlDocument.GetElementsByTagName("soap:Body")[0]; string innerObject = soapBody.InnerXml; XmlSerializer deserializer = new XmlSerializer(typeof(T)); using(StringReader reader = new StringReader(innerObject)) { return (T)deserializer.Deserialize(reader); } } 语句的行上收到“XML文档(1,2)中存在错误”的异常。

我错过了什么?如果这是解决问题的正确方法?

1 个答案:

答案 0 :(得分:0)

<orderID xmlns="http://serverurl.com">0000005555</orderID> <region xmlns="http://serverurl.com">Region11</region>

你的orderID结束标记丢失了。