Tôi đang cố gắng để deserialize XML sau:C# Có một lỗi trong tài liệu XML (2, 2)
<?xml version="1.0" encoding="UTF-8"?>
<XGResponse><Failure code="400">
Message id '1' was already submitted.
</Failure></XGResponse>
qua cuộc gọi này:
[...]
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);
}
}
để có được một thể hiện của lớp tương ứng:
//------------------------------------------------------------------------------
// <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;
}
}
}
}
Nhưng nó làm tăng lỗi There is an error in XML document (2, 2)
.
Tôi đã tìm một giải pháp cho điều này trong khoảng một giờ, nhưng nó không giúp được gì nhiều.
Tôi thậm chí đã cố gắng một thay đổi nhỏ mà không nên làm bất cứ điều gì:
public static T Deserialize<T>(string xml)
{
[...]
var xmlSerializer = new XmlSerializer(typeof(T), new XmlRootAttribute(typeof(T).Name));
[...]
}
Tuy nhiên, điều này không ngăn chặn các lỗi xảy ra. Nhưng vì nó chỉ đạt được để trả lại cho tôi một cá thể XMLGateResponse.XGResponse hoàn toàn trống rỗng (mọi phần tử/thuộc tính là null), nó không thực sự là một sự cải tiến.
Tôi biết loại câu hỏi này There is an error in XML document (2, 2)
đã được thảo luận rất nhiều, nhưng tôi thực sự không tìm được giải pháp phù hợp với tôi.
có ngoại lệ bên trong không? – Sayse
@Sayse " không được mong đợi." –
Serge
Tôi tưởng tượng nó sẽ làm với 'AnonymousType = true, Namespace =" http://tempuri.org/XMLGateResponse "', nhưng tôi không quá chắc chắn xin lỗi .. – Sayse