gpt4 book ai didi

c# - XmlSerializer 反序列化返回空数组

转载 作者:行者123 更新时间:2023-11-30 21:16:42 25 4
gpt4 key购买 nike

我正在尝试反序列化以下 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;
}
}

但是,在 var songs = _xmlSerializer.Deserialize(_xmlReader) as SearchResult; 上,反序列化执行成功,但 songs 变量不包含任何数据。如果我使用调试器进行检查,它会为数组中的所有值返回 Could not evaluate expression

有什么提示吗?谢谢。

最佳答案

您的 SearchResult 类需要一些修复。您真的很接近,代码只缺少一些元素名称和可序列化的属性。

这是一个有效的类:

[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; }
}

关于c# - XmlSerializer 反序列化返回空数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5079889/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com