XmlSerializer 反序列化返回空数组
我正在尝试反序列化以下 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>
使用以下类:
[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; }
}
和以下代码:
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;
}
}
但是,在 varongs = _xmlSerializer.Deserialize(_xmlReader) as SearchResult;
上,反序列化成功执行,但歌曲变量不包含任何数据。如果我使用调试器进行检查,它会返回数组中所有值的无法计算表达式
。
有什么提示吗?谢谢。
I'm trying to deserialize the following XML (excerpt):
<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;
, the Deserialization executes successfully, but the songs variable does not contain any data. If I inspect with the debugger, it returns Could not evaluate expression
for all the values in the array.
Any hints? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的
SearchResult
类需要一些修复。你真的很接近,代码只缺少一些元素名称和可序列化属性。这是一个有效的类:
Your
SearchResult
class needs some fixing. You're really close, the code is only missing a few element names and the serializable attributes.Here's a class that works:
我有一个正在序列化和反序列化的类。与您正在使用的属性相比...您有很多我没有的属性(在类上),但是您缺少的一个是 [Serialized]
我的属性看起来像这样:
所以从像这样的简单的东西开始,有效,然后一次添加一个新标签,您将很快找到哪个标签导致它损坏。
由于您在反序列化时遇到问题,因此这是我正在使用的代码。在另一堂课上,我有:
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]
Mine looks like this:
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: