Próbuję deserializowania następujące XML:C# Występuje błąd w dokumencie XML (2, 2)
<?xml version="1.0" encoding="UTF-8"?>
<XGResponse><Failure code="400">
Message id '1' was already submitted.
</Failure></XGResponse>
pośrednictwem tego połączenia:
[...]
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);
}
}
uzyskać instancję odpowiadająca klasa:
//------------------------------------------------------------------------------
// <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;
}
}
}
}
Podnosi jednak błąd There is an error in XML document (2, 2)
.
Szukałem rozwiązania tego od około godziny, ale to nie pomogło.
Próbowałem nawet niewielkie zmiany, które nie powinny niczego:
public static T Deserialize<T>(string xml)
{
[...]
var xmlSerializer = new XmlSerializer(typeof(T), new XmlRootAttribute(typeof(T).Name));
[...]
}
Jednak to nie przeszkadza błąd wydarzy. Ale ponieważ wystarczy, że zwróci mi instancję XMLGateResponse.XGResponse całkowicie pustą (wszystkie elementy/atrybuty są puste), to naprawdę nie jest poprawa.
Znam ten rodzaj pytania There is an error in XML document (2, 2)
było już dużo dyskutowane, ale naprawdę nie znalazłem rozwiązania, które by dla mnie działało.
Czy istnieje wyjątek wewnętrzny? – Sayse
@Sayse " nie było oczekiwane." –
Serge
Wyobrażam sobie, że ma to coś wspólnego z 'AnonymousType = true, Namespace =" http://tempuri.org/XMLGateResponse "', ale nie jestem zbyt pewny, przepraszam .. – Sayse