Ошибка десериализации c # xml в документе xml (1,40) - PullRequest
0 голосов
/ 02 марта 2019

У меня есть строка, которую нужно десериализовать, и это ответ

<?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 голосов
/ 02 марта 2019

Небольшое изменение в вашем 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; }
    }

, затем попробуйте ее кодбуду работатьЯ проверил это локально.Удачного кодирования ...

...