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

标签 c# xml-serialization 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; 上,反序列化成功执行,但ongong变量不包含任何数据。如果我使用调试器进行检查,它会返回无法计算数组中所有值的表达式

有什么提示吗?谢谢。

最佳答案

您的 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/

相关文章:

c# - 使用 XmlSerializer 反序列化 xml 文件时出错

c# - 将对象序列化为 XML

c# - 如何使用c#获取任务栏中应用程序的进程名称?

c# - .NET 中快速紧凑的对象序列化

.net - 为 .net 类生成两个不同的 xml 序列化

java - 使用 apache 公共(public)配置 XMLConfiguration 格式化 XML 输出

c# - 捕获外部过程中的错误

c# - 如何在 Windows 8.1 中获取 MessageBox 图标

C# byte array xml序列化

c# - 有没有办法删除*仅* xmlns :xsd namespace when serializing a XML?