XmlSerializer反序列化返回空数组

时间:2011-02-22 15:09:26

标签: 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;
        }
    }

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

任何提示?感谢。

2 个答案:

答案 0 :(得分:4)

您的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; }
}

答案 1 :(得分:0)

我有一个类,我正在序列化和反序列化。与你正在使用的属性相比......你有很多我没有的(在课堂上),但你缺少的是[Serializable]

我的样子如下:

[Serializable]
public class Settings
{
    [XmlElement]
    public int Version { get; set; }

    [XmlElement]
    public string Name { get; set; }

    // more settings
}

首先从像这样简单的东西开始,然后运行,然后一次添加一个新标签,你会很快发现哪一个导致它破坏。

由于您在反序列化时遇到问题,因此这是我正在使用的代码。在另一节课中,我有:

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