xml文档中的C#xml反序列化错误(1,40)

时间:2019-03-02 13:07:27

标签: c# xml xml-deserialization

我有一个需要反序列化的字符串,这是一个响应

<?xml version=\"1.0\" encoding=\"utf-8\"?>
<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">
<soap:Body>
  <TestResponse xmlns=\"http://tempuri.org/\">
     <TestResult>
            <code>1</code>
            <msg>Message was successfully sent</msg>
            <sent />
    </TestResult>
  </TestResponse>
</soap:Body>
</soap:Envelope>

这是我的反序列化代码

                string s = System.Text.ASCIIEncoding.ASCII.GetString(result);
                Test_Response resp = new Test_Response();
                using (var stringReader = new System.IO.StringReader(s))
                {
                    var serializer = new XmlSerializer(typeof(Test_Response));
                    resp = (Test_Response)serializer.Deserialize(stringReader);
                }
                return s;

这是我的课程

   [Serializable(), XmlRoot("TestResult")]
   public class Test_Response
    {
        public string code { get; set; }
        public string msg { get; set; }
        public string sent { get; set; }
    }

然后错误消息是

  

XML文档(1,40)中存在错误。

内部例外是

  

<Envelope xmlns='http://www.w3.org/2003/05/soap-envelope'> was not expected.

谁能帮助我。谢谢

1 个答案:

答案 0 :(得分:2)

您的XML文件中的更改很小:/末尾有额外的xmlns

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <soap:Body>
    <TestResponse xmlns="http://tempuri.org/">
      <TestResult>
        <code>1</code>
        <msg>Message was successfully sent</msg>
        <sent />
      </TestResult>
    </TestResponse>

  </soap:Body>
</soap:Envelope>

并创建如下模型:

[XmlRoot(ElementName = "TestResult", Namespace = "http://tempuri.org/")]
    public class TestResult
    {
        [XmlElement(ElementName = "code", Namespace = "http://tempuri.org/")]
        public string Code { get; set; }
        [XmlElement(ElementName = "msg", Namespace = "http://tempuri.org/")]
        public string Msg { get; set; }
        [XmlElement(ElementName = "sent", Namespace = "http://tempuri.org/")]
        public string Sent { get; set; }
    }

    [XmlRoot(ElementName = "TestResponse", Namespace = "http://tempuri.org/")]
    public class TestResponse
    {
        [XmlElement(ElementName = "TestResult", Namespace = "http://tempuri.org/")]
        public TestResult TestResult { get; set; }
        [XmlAttribute(AttributeName = "xmlns")]
        public string Xmlns { get; set; }
    }

    [XmlRoot(ElementName = "Body", Namespace = "http://www.w3.org/2003/05/soap-envelope")]
    public class Body
    {
        [XmlElement(ElementName = "TestResponse", Namespace = "http://tempuri.org/")]
        public TestResponse TestResponse { get; set; }
    }

    [XmlRoot(ElementName = "Envelope", Namespace = "http://www.w3.org/2003/05/soap-envelope")]
    public class Envelope
    {
        [XmlElement(ElementName = "Body", Namespace = "http://www.w3.org/2003/05/soap-envelope")]
        public Body Body { get; set; }
        [XmlAttribute(AttributeName = "soap", Namespace = "http://www.w3.org/2000/xmlns/")]
        public string Soap { get; set; }
        [XmlAttribute(AttributeName = "xsi", Namespace = "http://www.w3.org/2000/xmlns/")]
        public string Xsi { get; set; }
        [XmlAttribute(AttributeName = "xsd", Namespace = "http://www.w3.org/2000/xmlns/")]
        public string Xsd { get; set; }
    }

然后尝试您的代码即可。我已经在本地测试过。 编码愉快...