XML 反序列化不起作用

发布于 2025-01-04 01:59:15 字数 806 浏览 2 评论 0原文

我需要将 XML 文件反序列化为对象。 XML 内容:

  <Players dealerId="2">
    <Player id="1">
      <ScreenName>JetYeo</ScreenName>
    </Player>
    <Player id="2">
      <ScreenName>Test</ScreenName>
    </Player>
  </Players>

我定义了一个对象类:

[Serializable()]
[XmlRoot("Players")]
public class Players
{
    [XmlAttribute("dealerId")]
    public int DealerId { get; set; }
    [XmlArrayItem("Player", typeof(Player))]
    public Player[] Players { get; set; }
}

[Serializable()]
[XmlRoot("Player")]
public class Player
{
    [XmlAttribute("id")]
    public int Id { get; set; }
    [XmlElement("ScreenName")]
    public string ScreenName { get; set; }
}

但是,反序列化不起作用: Players 数组为 null。请帮我解决它。谢谢。

I need to deserialize a XML file to a object. The XML contents:

  <Players dealerId="2">
    <Player id="1">
      <ScreenName>JetYeo</ScreenName>
    </Player>
    <Player id="2">
      <ScreenName>Test</ScreenName>
    </Player>
  </Players>

I define a object class:

[Serializable()]
[XmlRoot("Players")]
public class Players
{
    [XmlAttribute("dealerId")]
    public int DealerId { get; set; }
    [XmlArrayItem("Player", typeof(Player))]
    public Player[] Players { get; set; }
}

[Serializable()]
[XmlRoot("Player")]
public class Player
{
    [XmlAttribute("id")]
    public int Id { get; set; }
    [XmlElement("ScreenName")]
    public string ScreenName { get; set; }
}

However, deserialization does not work: Players array is null. Please help me to solve it. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

冷弦 2025-01-11 01:59:15

识别此类问题的最佳方法是实际上反转序列化逻辑。
使用与 XML 中的数据相匹配的数据创建对象。然后尝试对其进行序列化,以便输出与您想要反序列化的数据相同。

这样,您实际上可以“查看”可应用于属性的可用属性的效果。

如果它序列化相同,您也可以反序列化它。

The best way to identify this kind of problems is to actually reverse your serialization logic.
Create the object with the data that matches the data you have in your XML. Then try to serialize it so that the output is the same as the data you would like to deserialize.

This way, you can actually 'see' the effect of the available attributes that you can apply on the properties.

If it serializes the same, you can also deserialize it.

囚我心虐我身 2025-01-11 01:59:15

失败是因为属性错误;区别在于 XmlArrayItem 需要两级关系 (...);因此,对于 Players 来说,它应该是:

[XmlElement("Player")]
public Player[] Players { get; set; }

尽管就我个人而言,我更喜欢:(

private readonly List<Player> players = new List<Player>();
[XmlElement("Player")]
public List<Player> Players { get { return players; } }

即没有 set,并且使用列表而不是数组)

,甚至是延迟实例化:

private List<Player> players;
[XmlElement("Player")]
public List<Player> Players {
    get { return players ?? (players = new List<Player>()); }
}

It fails because the attributes are wrong; the difference is that XmlArrayItem expects an two-level relationship (<Players><Players><Player .../>...</Players></Players>); hence for Players it should be:

[XmlElement("Player")]
public Player[] Players { get; set; }

although personally, I'd prefer:

private readonly List<Player> players = new List<Player>();
[XmlElement("Player")]
public List<Player> Players { get { return players; } }

(i.e. no set, and a list instead of an array)

or even the lazily-instantiated:

private List<Player> players;
[XmlElement("Player")]
public List<Player> Players {
    get { return players ?? (players = new List<Player>()); }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文