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)
これに対する解決策を約1時間探しましたが、あまり役に立ちませんでした。

私は何もしないはずのわずかな変更を試みました:

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

それでも、これはエラーの発生を防ぎます。しかし、完全に空の XMLGateResponse.XGResponse インスタンス (すべての要素/属性が null) を返すことしかできないため、実際には改善されていません。

この種の質問There is an error in XML document (2, 2)がすでに多く議論されていることは知っていますが、私にとってうまくいく解決策は本当に見つかりませんでした.

4

2 に答える 2

12

間違った型にデシリアライズしようとすると、同じエラーが発生する可能性があります。
たとえば、あなたが電話した場合

Deserialize<object>(myXml)

また

Deserialize<Failure>(myXml)

1) 回答がすでに提供されている場合、および 2) 回答が質問者が求めたとおりのものではない場合に、Q に回答するのは悪い習慣であることは承知しています。しかし、質問者が抱えていた問題とまったく同じではない問題で、他の誰かがここにたどり着くまでの時間を節約できると思います。

于 2014-03-06T07:58:42.453 に答える
10

XGResponse はXmlRootAttribute、デフォルトの名前空間名を指定する で装飾されていますが、ドキュメントはそうではありません。

この名前空間宣言を削除するか、xmlns="http://tempuri.org/XMLGateResponse"xml のルート要素に追加してください

于 2013-08-22T10:20:05.223 に答える