XmlSerializer 反序列化返回空数组

发布于 2024-10-18 14:12:42 字数 2908 浏览 2 评论 0原文

我正在尝试反序列化以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤单情人 2024-10-25 14:12:42

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

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:

[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; }
}
自由范儿 2024-10-25 14:12:42

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

我的属性看起来像这样:

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

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:

[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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文