XML 文档中存在错误 (0, 0) - 将 XML 反序列化为对象时出错

Posted

技术标签:

【中文标题】XML 文档中存在错误 (0, 0) - 将 XML 反序列化为对象时出错【英文标题】:There is an error in XML document (0, 0) - Error Deserializing XML to object 【发布时间】:2020-11-24 12:49:50 【问题描述】:

请建议我如何将响应映射到类中,因为从每个示例中我遇到的正文总是没有像 core:transactionResponse 这样的符号

我的代码:

string fileName = @"C:\Users\Lenovo\Downloads\GLResponseXml.xml";
        XDocument xDoc = XDocument.Load(fileName);

        var unwrappedResponse = xDoc.Descendants((XNamespace)"http://schemas.xmlsoap.org/soap/envelope/" + "Body")
                                .First()
                                .FirstNode;

        XmlSerializer xmlSerializer = new XmlSerializer(typeof(TransactionResponse));
        TransactionResponse response = (TransactionResponse)xmlSerializer.Deserialize(xDoc.Descendants((XNamespace)"http://schemas.xmlsoap.org/soap/envelope/" + "Body")
            .First()
            .FirstNode
            .CreateReader()
        );

为了解除这个soap xml:

    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dpss0="bons">
    <soapenv:Header/>
    <soapenv:Body>
        <core:transactionResponse xmlns:bo="http://service.example.co.id/core/bo" xmlns:core="http://service.example.co.id/core">
            <response>
                <header>
                    <coreJournal>149326</coreJournal>
                </header>
                <content xsi:type="bo:OKMessage">
                    <message/>
                </content>
            </response>
        </core:transactionResponse>
    </soapenv:Body>
   </soapenv:Envelope>

但我收到此错误:InvalidOperationException: &lt;transactionResponse xmlns='http://service.example.co.id/core'&gt; was not expected.

我正在将响应映射到此类:

public class GLXmlResponse

    [XmlRoot(ElementName = "response")]
    public class Response
    
        [XmlElement(ElementName = "header")]
        public Header Header  get; set; 
        [XmlElement(ElementName = "content")]
        public Content Content  get; set; 
    

    [XmlRoot(ElementName = "header")]
    public class Header
    
        [XmlElement(ElementName = "coreJournal")]
        public string CoreJournal  get; set; 
    

    [XmlRoot(ElementName = "content")]
    public class Content
    
        [XmlElement(ElementName = "message")]
        public string Message  get; set; 
        [XmlAttribute(AttributeName = "type", Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
        public string Type  get; set; 
    

    [XmlRoot(ElementName = "transactionResponse", Namespace = "http://service.example.co.id/core")]
    public class TransactionResponse
    
        [XmlElement(ElementName = "response")]
        public Response Response  get; set; 
        [XmlAttribute(AttributeName = "bo", Namespace = "http://www.w3.org/2000/xmlns/")]
        public string Bo  get; set; 
        [XmlAttribute(AttributeName = "core", Namespace = "http://www.w3.org/2000/xmlns/")]
        public string Core  get; set; 
    

    [XmlRoot(ElementName = "Body", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
    public class Body
    
        [XmlElement(ElementName = "transactionResponse", Namespace = "http://service.example.co.id/core")]
        public TransactionResponse TransactionResponse  get; set; 
    

    [XmlRoot(ElementName = "Envelope", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
    public class Envelope
    
        [XmlElement(ElementName = "Header", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
        public string Header  get; set; 
        [XmlElement(ElementName = "Body", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
        public Body Body  get; set; 
        [XmlAttribute(AttributeName = "soapenv", Namespace = "http://www.w3.org/2000/xmlns/")]
        public string Soapenv  get; set; 
        [XmlAttribute(AttributeName = "soapenc", Namespace = "http://www.w3.org/2000/xmlns/")]
        public string Soapenc  get; set; 
        [XmlAttribute(AttributeName = "xsd", Namespace = "http://www.w3.org/2000/xmlns/")]
        public string Xsd  get; set; 
        [XmlAttribute(AttributeName = "xsi", Namespace = "http://www.w3.org/2000/xmlns/")]
        public string Xsi  get; set; 
        [XmlAttribute(AttributeName = "dpss0", Namespace = "http://www.w3.org/2000/xmlns/")]
        public string Dpss0  get; set; 
    

在正确设置类对象方面需要帮助

【问题讨论】:

XmlRoot 和 XmlElement 具有需要设置的命名空间属性。它可以是 URL 或空字符串“”。您可能需要添加空字符串才能使代码正常工作。 Xml 序列化很难调试。所以我通常做的是从根类开始,注释掉类中的所有属性,让代码只与根类一起工作。然后慢慢取出cmets,找出错误发生在哪里。 【参考方案1】:

我无法重现您所看到的确切错误,因此您可能需要从最小的重现中进行检查;我正在使用:

var env = (GLXmlResponse.Envelope)new XmlSerializer(typeof(GLXmlResponse.Envelope))
    .Deserialize(new StringReader(xml));        
System.Console.WriteLine(env.Body.TransactionResponse.Response.Header.CoreJournal);

地点:

string xml = @"<soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:soapenc=""http://schemas.xmlsoap.org/soap/encoding/"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:dpss0=""bons"">
<soapenv:Header/>
<soapenv:Body>
    <core:transactionResponse xmlns:bo=""http://service.example.co.id/core/bo"" xmlns:core=""http://service.example.co.id/core"">
        <response>
            <header>
                <coreJournal>149326</coreJournal>
            </header>
            <content xsi:type=""bo:OKMessage"">
                <message/>
            </content>
        </response>
    </core:transactionResponse>
</soapenv:Body></soapenv:Envelope>";

其中只有一个 null 代表 .Responsethat 的原因是 this 实际上是在空的命名空间中(前缀不被继承),所以你需要

[XmlElement(ElementName = "response", Namespace = "")]
public Response Response  get; set; 

但是,这会导致 xml 中的 OKMessage 出现问题。如果我注释掉&lt;content&gt;,那么它可以工作,我可以看到输出149326

但是,首先将 xml 复制到剪贴板,编辑 -> 选择性粘贴 -> 将 XML 粘贴为类,然后查看它生成的内容,然后从那里开始工作,这可能会更简单。

【讨论】:

粘贴特殊 xml 生成的类时,我仍然遇到同样的错误

以上是关于XML 文档中存在错误 (0, 0) - 将 XML 反序列化为对象时出错的主要内容,如果未能解决你的问题,请参考以下文章

System.InvalidOperationException: 'XML 文档 (1, 1) 中存在错误。'

新建maven工程时pom.xml报错

反序列化对对象的 xml 响应时,xml 文档中存在错误

BizTalk - 设置电子邮件 contentType 导致错误:XML 文档中存在错误 (1, 1)

xml教程

java xml解析方式(DOMSAXJDOMDOM4J)