【问题标题】:There is an error in xml document when deserializing xml response to object反序列化对对象的 xml 响应时,xml 文档中存在错误
【发布时间】:2021-11-05 21:29:21
【问题描述】:

我正在尝试反序列化来自 Web 服务的 xml 响应。这是回复

<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Body>
        <ConvertToTypeResponse xmlns="http://tempuri.org/">
            <ConvertToTypeResult>8959459395</ConvertToTypeResult>
        </ConvertToTypeResponse>
    </soap:Body>
</soap:Envelope>

我尝试将上面的 xml 反序列化到这个信封。

[XmlRoot(ElementName = "ConvertToTypeResponse")]
public class ConvertToTypeResponse
{

    [XmlElement(ElementName = "ConvertToTypeResult", Namespace = "http://tempuri.org/")]
    public int ConvertToTypeResult { get; set; }

    [XmlAttribute(AttributeName = "xmlns")]
    public string Xmlns { get; set; }

    [XmlText]
    public string Text { get; set; }
}

[XmlRoot(ElementName = "Body")]
public class Body
{

    [XmlElement(ElementName = "ConvertToTypeResponse", Namespace = "http://tempuri.org/")]
    public ConvertToTypeResponse ConvertToTypeResponse { get; set; }
}

[XmlRoot(ElementName = "Envelope")]
public class Envelope
{

    [XmlElement(ElementName = "Body")]
    public Body Body { get; set; }

    [XmlAttribute(AttributeName = "soap")]
    public string Soap { get; set; }

    [XmlAttribute(AttributeName = "xsi")]
    public string Xsi { get; set; }

    [XmlAttribute(AttributeName = "xsd")]
    public string Xsd { get; set; }

    [XmlText]
    public string Text { get; set; }
}

但我收到此错误There is an error in XML document (1, 40).

IRestResponse response = _client.Execute(_request);
var c = response.Content;
if (response.IsSuccessful)
{
    XmlSerializer secrializer = new XmlSerializer(typeof(Envelope));
    using (StringReader reader = new StringReader(c))
    {
        var test = (Envelope)secrializer.Deserialize(reader);
    }
}

我已经检查了结构,但我很难在模型类中找到任何不匹配的地方。 xml &lt;?xml version="1.0" encoding="UTF-8"?&gt; 的第一行是否可能是问题所在,因为模型类中没有提供它。

【问题讨论】:

  • ConvertToTypeResponse 的末尾似乎缺少一个右尖括号 - StackOverflow 语法突出显示已拾取。
  • 此外,ConvertToTypeResultConvertToNubanResult 不匹配,这是无效的。如果这确实是从您所依赖的 Web 服务返回的响应,则该服务已损坏并返回无效内容。
  • @TomW 发布问题时出错,我现在更新了
  • @KlausGütter 是的,这是第一个&lt;?xml version="1.0" encoding="UTF-8"?&gt;

标签: c# xml soap


【解决方案1】:

更改模型类以包含必要的命名空间修复了问题

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

[XmlRoot(ElementName = "Body", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public class Body
{
    [XmlElement(ElementName = "ConvertToTypeResponse", Namespace = "http://tempuri.org/")]
    public ConvertToTypeResponse ConvertToTypeResponse { get; set; }
}

[XmlRoot(ElementName = "Envelope", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public class Envelope
{
    [XmlElement(ElementName = "Body", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
    public Body Body { 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; }
    [XmlAttribute(AttributeName = "soap", Namespace = "http://www.w3.org/2000/xmlns/")]
    public string Soap { get; set; }
}

【讨论】:

    猜你喜欢
    • 2020-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多