【问题标题】:C# There is an error in XML document (2, 2)C# XML 文档有错误 (2, 2)
【发布时间】:2013-08-25 00:18:53
【问题描述】:

我正在尝试反序列化以下 XML:

<?xml version="1.0" encoding="UTF-8"?>
<XGResponse><Failure code="400">
    Message id &apos;1&apos; was already submitted.
</Failure></XGResponse>

通过这个电话:

[...]
    var x = SerializationHelper.Deserialize<XMLGateResponse.XGResponse>(nResp);
[...]        

public static T Deserialize<T>(string xml)
{
    using (var str = new StringReader(xml))
    {
        var xmlSerializer = new XmlSerializer(typeof(T));
        return (T)xmlSerializer.Deserialize(str);
    }
}

获取对应类的实例:

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:4.0.30319.18052
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

using System.Xml.Serialization;

// 
// This source code was auto-generated by xsd, Version=4.0.30319.1.
// 

namespace XMLGateResponse
{

    /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://tempuri.org/XMLGateResponse")]
    [System.Xml.Serialization.XmlRootAttribute(Namespace = "http://tempuri.org/XMLGateResponse", IsNullable = false)]
    public partial class XGResponse
    {

        private object[] itemsField;

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("Failure", typeof(Failure))]
        [System.Xml.Serialization.XmlElementAttribute("Success", typeof(Success))]
        public object[] Items
        {
            get
            {
                return this.itemsField;
            }
            set
            {
                this.itemsField = value;
            }
        }
    }

    /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://tempuri.org/XMLGateResponse")]
    [System.Xml.Serialization.XmlRootAttribute(Namespace = "http://tempuri.org/XMLGateResponse", IsNullable = false)]
    public partial class Failure
    {

        private string codeField;

        private string titleField;

        private string valueField;

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute(DataType = "NMTOKEN")]
        public string code
        {
            get
            {
                return this.codeField;
            }
            set
            {
                this.codeField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string title
        {
            get
            {
                return this.titleField;
            }
            set
            {
                this.titleField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlTextAttribute()]
        public string Value
        {
            get
            {
                return this.valueField;
            }
            set
            {
                this.valueField = value;
            }
        }
    }

    /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://tempuri.org/XMLGateResponse")]
    [System.Xml.Serialization.XmlRootAttribute(Namespace = "http://tempuri.org/XMLGateResponse", IsNullable = false)]
    public partial class Success
    {

        private string titleField;

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string title
        {
            get
            {
                return this.titleField;
            }
            set
            {
                this.titleField = value;
            }
        }
    }
}

但它会引发错误There is an error in XML document (2, 2)
我已经为这个问题寻找了大约一个小时的解决方案,但没有太大帮助。

我什至尝试了一个不应该做任何事情的轻微改变:

public static T Deserialize<T>(string xml)
{
    [...]
        var xmlSerializer = new XmlSerializer(typeof(T), new XmlRootAttribute(typeof(T).Name));
    [...]
}

但是,这确实可以防止错误发生。但是因为它只实现了返回一个完全空的 XMLGateResponse.XGResponse 实例(每个元素/属性都是空的),所以这并不是一个真正的改进。

我知道There is an error in XML document (2, 2)这种问题已经讨论了很多,但我真的没有找到适合我的解决方案。

【问题讨论】:

  • 有内部异常吗?
  • @Sayse " 不是预期的。"
  • 我想它与AnonymousType = true, Namespace = "http://tempuri.org/XMLGateResponse"有关,但我不太确定抱歉..

标签: c# xml-deserialization


【解决方案1】:

如果您尝试反序列化为错误的类型,您可能会遇到同样的错误。
例如,如果您调用

Deserialize<object>(myXml)

Deserialize<Failure>(myXml)

我知道在 1) 已经提供答案并且 2) 答案与提问者所要求的不完全一致时,回答 Q 是一种不好的做法;但我认为这可能会为其他人找到与提问者不完全一样的问题找到方法节省一些时间。

【讨论】:

    【解决方案2】:

    XGResponse 用 XmlRootAttribute 修饰,它指定了默认的命名空间名称,但您的文档没有。

    要么删除此命名空间声明,要么将 xmlns="http://tempuri.org/XMLGateResponse" 添加到您的 xml 的根元素中

    【讨论】:

    • 坦克它工作。你知道为什么这个装饰器是由实用工具 XSD.exe 设置的吗?因为我以前从来没有遇到过这个问题。
    • 默认情况下,它会在文档中强制指定 XML 命名空间。它们看起来很麻烦,但实际上它们对于分区文档非常有用,并且使用它们是一个很好的实践。考虑一下您是否拿走了别人的文档并将其嵌套在自己的文档中。两个文档都有
      元素,但标题元素的形式不同。如果嵌套文档指定了命名空间,则在按元素名称搜索时不会混淆。
    • 另外,请考虑@Sayse 在问题 cmets 中指出的 XmlTypeAttribute。当类型不是根元素时,这具有相同的功能。
    • 如果我没有命名空间怎么办?我尝试删除 .XMLRootAttribute 但它仍然给了我异常。我的代码中没有其他地方
    猜你喜欢
    • 1970-01-01
    • 2019-01-14
    • 2020-08-13
    • 2023-03-20
    • 1970-01-01
    • 2015-03-10
    • 1970-01-01
    • 1970-01-01
    • 2021-08-28
    相关资源
    最近更新 更多