私は、次のXMLシリアル化を解除しようとしている:この呼び出しを通じてC#の(2、2)
<?xml version="1.0" encoding="UTF-8"?>
<XGResponse><Failure code="400">
Message id '1' 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)
は既に多くのことが議論されていましたが、実際には私のために働く解決策は見つかりませんでした。
内部例外がありますか? – Sayse
@Sayse "は期待されていませんでした。 –
Serge
私はそれが 'AnonymousType = true、Namespace =" http://tempuri.org/XMLGateResponse "とは想像していましたが、あまりにも申し訳ありません。 – Sayse