10

我正在尝试反序列化以下 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)已经讨论了很多,但我真的没有找到适合我的解决方案。

4

2 回答 2

12

如果您尝试反序列化为错误的类型,您可能会收到相同的错误。
例如,如果你打电话

Deserialize<object>(myXml)

或者

Deserialize<Failure>(myXml)

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

于 2014-03-06T07:58:42.453 回答
10

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

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

于 2013-08-22T10:20:05.223 回答