【问题标题】:c# xml deserialized error in xml document (1,40)c# xml文档中的xml反序列化错误(1,40)
【发布时间】:2019-03-02 13:07:27
【问题描述】:

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

<?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)。

内部异常是

&lt;Envelope xmlns='http://www.w3.org/2003/05/soap-envelope'&gt; was not expected.

谁能帮帮我。谢谢

【问题讨论】:

    标签: c# xml xml-deserialization


    【解决方案1】:

    您的 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; }
        }
    

    然后试试你的代码,它会工作的。我在本地对此进行了测试。 快乐编码...

    【讨论】:

    • 如果这有助于解决您的问题,您可以将其标记为答案。
    • 我很想知道是什么原因造成的。
    猜你喜欢
    • 1970-01-01
    • 2011-06-11
    • 2016-04-28
    • 2018-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多