[英]XmlSerializer Deserialize returns empty array
I'm trying to deserialize the following XML (excerpt): 我正在尝试反序列化以下XML(摘录):
<NSArray>
<Song id="23507" type="Song">
<title>Waking The Demon</title>
<artist id="17" type="Artist">
<nameWithoutThePrefix>Bullet For My Valentine</nameWithoutThePrefix>
<useThePrefix>false</useThePrefix>
<name>Bullet For My Valentine</name>
</artist>
</Song>
<Song id="3663" type="Song">
<title>Hand Of Blood</title>
<artist id="17" type="Artist"/>
</Song>
<Song id="59226" type="Song">
<title>Your Betrayal</title>
<artist id="17" type="Artist"/>
</Song>
</NSArray>
with the following classes: 具有以下类别:
[GeneratedCode("xsd", "4.0.30319.1")]
[DebuggerStepThrough]
[XmlType(AnonymousType = true)]
[XmlRoot(ElementName = "NSArray", Namespace = "", IsNullable = false)]
public class SearchResult
{
[XmlElement("Song", Form = XmlSchemaForm.Unqualified)]
public Song[] Items { get; set; }
}
[GeneratedCode("xsd", "4.0.30319.1")]
[DebuggerStepThrough]
[XmlType(AnonymousType = true)]
[XmlRoot(Namespace = "", IsNullable = false)]
public class Song
{
[XmlElement(Form = XmlSchemaForm.Unqualified)]
public string Title { get; set; }
[XmlElement("artist", Form = XmlSchemaForm.Unqualified)]
public Artist Artist { get; set; }
[XmlAttribute]
public string Type { get; set; }
[XmlAttribute]
public string Id { get; set; }
}
[GeneratedCode("xsd", "4.0.30319.1")]
[DebuggerStepThrough]
[XmlType(AnonymousType = true)]
public class Artist
{
[XmlElement(Form = XmlSchemaForm.Unqualified)]
public string NameWithoutThePrefix { get; set; }
[XmlElement(Form = XmlSchemaForm.Unqualified)]
public string UseThePrefix { get; set; }
[XmlElement(Form = XmlSchemaForm.Unqualified)]
public string Name { get; set; }
[XmlAttribute]
public string Type { get; set; }
[XmlAttribute]
public string Id { get; set; }
}
and the following code: 和以下代码:
var request = WebRequest.Create(string.Format("http://myurl.com");
request.BeginGetResponse(GetEventResponseCallback, request);
private void GetEventResponseCallback(IAsyncResult result)
{
var request = (HttpWebRequest)result.AsyncState;
var response = request.EndGetResponse(result);
if (response.GetResponseStream() == null) return;
using (var stream = response.GetResponseStream())
{
_xmlReader = XmlReader.Create(stream);
var songs = _xmlSerializer.Deserialize(_xmlReader) as SearchResult;
}
}
However, on var songs = _xmlSerializer.Deserialize(_xmlReader) as SearchResult;
但是,在
var songs = _xmlSerializer.Deserialize(_xmlReader) as SearchResult;
, the Deserialization executes successfully, but the songs variable does not contain any data. ,反序列化将成功执行,但是songs变量不包含任何数据。 If I inspect with the debugger, it returns
Could not evaluate expression
for all the values in the array. 如果我使用调试器进行检查,它将返回
Could not evaluate expression
为数组中的所有值Could not evaluate expression
。
Any hints? 有什么提示吗? Thanks.
谢谢。
Your SearchResult
class needs some fixing. 您的
SearchResult
类需要一些修复。 You're really close, the code is only missing a few element names and the serializable attributes. 您真的很接近,代码仅缺少一些元素名称和可序列化的属性。
Here's a class that works: 这是一个有效的课程:
[GeneratedCode("xsd", "4.0.30319.1")]
[DebuggerStepThrough]
[XmlType(AnonymousType = true)]
[XmlRoot(ElementName = "NSArray", Namespace = "", IsNullable = false)]
[Serializable]
public class SearchResult
{
[XmlElement("Song", Form = XmlSchemaForm.Unqualified)]
public Song[] Items { get; set; }
}
[GeneratedCode("xsd", "4.0.30319.1")]
[DebuggerStepThrough]
[XmlType(AnonymousType = true)]
[XmlRoot(ElementName="Song", Namespace = "", IsNullable = false)]
[Serializable]
public class Song
{
[XmlElement("title", Form = XmlSchemaForm.Unqualified)]
public string Title { get; set; }
[XmlElement("artist", Form = XmlSchemaForm.Unqualified)]
public Artist Artist { get; set; }
[XmlAttribute("type")]
public string Type { get; set; }
[XmlAttribute("id")]
public string Id { get; set; }
}
[GeneratedCode("xsd", "4.0.30319.1")]
[DebuggerStepThrough]
[XmlType(AnonymousType = true)]
[Serializable]
public class Artist
{
[XmlElement("nameWithoutThePrefix", Form = XmlSchemaForm.Unqualified)]
public string NameWithoutThePrefix { get; set; }
[XmlElement("useThePrefix", Form = XmlSchemaForm.Unqualified)]
public string UseThePrefix { get; set; }
[XmlElement("name", Form = XmlSchemaForm.Unqualified)]
public string Name { get; set; }
[XmlAttribute("type")]
public string Type { get; set; }
[XmlAttribute("id")]
public string Id { get; set; }
}
I have a class that I am serializing and deserializing. 我有一个要序列化和反序列化的类。 Compared to the attributes you are using... you have a lot that I don't have (on the class), but one that you are missing is [Serializable]
与您使用的属性相比...(在课堂上)我没有很多,但是[Serializable]是您所缺少的
Mine looks like this: 我的看起来像这样:
[Serializable]
public class Settings
{
[XmlElement]
public int Version { get; set; }
[XmlElement]
public string Name { get; set; }
// more settings
}
So start with something simple like this, that works, then add in each new tag one at a time, and you will quickly find which one is causing it to break. 因此,从类似这样的简单操作开始,然后一次添加一个新标签,您会很快发现哪个标签造成了损坏。
Since you are having trouble deserializing, here is the code I am using. 由于您在反序列化方面遇到麻烦,因此这是我正在使用的代码。 In another class, I have:
在另一堂课中,我有:
public string FilePath { get; set; }
public Settings LoadSettings()
{
XmlSerializer serializer = new XmlSerializer(typeof(Settings));
Settings settings = null;
using(TextReader reader = new StreamReader(this.FilePath))
{
settings = (Settings)serializer.Deserialize(reader);
}
return settings;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.