I'm trying to deserialize the following XML :
<?xml version="1.0" encoding="UTF-8"?>
<XGResponse><Failure code="400">
Message id '1' 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.
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.