11

I'm trying to deserialize the following XML :

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

through this call :

[...]
    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);
    }
}

to get an instance of the corresponding class :

//------------------------------------------------------------------------------
// <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;
            }
        }
    }
}

But it raise the error There is an error in XML document (2, 2).
I've looked for a solution to this for about an hour now, but it didn't help much.

I even tried a slight change which should not do anything :

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

Yet, this does prevent the error to happen. But since it only achieve to return me a XMLGateResponse.XGResponse instance fully empty (every elements/attributes are null), it's not really an improvement.

I know this kind of question There is an error in XML document (2, 2) has been discussed a lot already, but I really didn't find a solution that worked for me.

Serge
  • 6,554
  • 5
  • 30
  • 56

2 Answers2

12

If you try to deserialise to the wrong type you might get the same error.
For instance if you call

Deserialize<object>(myXml)

or

Deserialize<Failure>(myXml)

I know it is a bad practice to answer the Q when 1)the answer is already provided and 2)the answer is not exactly what the questioner asked for; but I think it might save some time for someone else finding their way here with a problem not exactly like the questioner had.

Thomas Ayoub
  • 29,063
  • 15
  • 95
  • 142
LosManos
  • 7,195
  • 6
  • 56
  • 107
10

XGResponse is decorated with an XmlRootAttribute that specifies a default namspace name but your document does not.

Either remove this namespace declaration or add xmlns="http://tempuri.org/XMLGateResponse" to the root element of you xml

Gusdor
  • 14,001
  • 2
  • 52
  • 64
  • Tanks it works. Do you have any idea why this decorator was set by the utility tool XSD.exe? Because I haven't ever had that problem before. – Serge Aug 22 '13 at 10:24
  • 1
    It does it by default to enforce specification of an XML namespace in your document. They look like a pain in the arse but actually they are very useful for partitioning documents and using them is a great practice to follow. Consider if you took someone else's document and nested it inside your own. Both documents have
    elements but the header elements are different in form. If the nested document had a namespace specified, them there would be no confusion when search by element name.
    – Gusdor Aug 22 '13 at 10:28
  • Also, consider the XmlTypeAttribute as noted by @Sayse in the question comments. This has the same function for when the type is not the root element. – Gusdor Aug 22 '13 at 10:29
  • What if I don't have a namespace? I tried removing the .XMLRootAttribute but it still gives me the exception. It's nowhere else in my code – Kala J Apr 08 '14 at 19:11