将对象序列化为 XML 元素 (c#)

发布于 2024-12-28 11:37:19 字数 247 浏览 1 评论 0原文

[System.Xml.Serialization.XmlRootAttribute("player", IsNullable = false)]
 public class Player
 {
  ...
  }

创建和序列化 new Player() 而不设置任何属性给了我 XML 元素,但我想获取

[System.Xml.Serialization.XmlRootAttribute("player", IsNullable = false)]
 public class Player
 {
  ...
  }

Creating and serializing new Player() whitout setting any properties gives me
the XML Element<player/> but I would like to get <player></player>.

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

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

发布评论

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

评论(3

弥枳 2025-01-04 11:37:19

就 XML 而言, 相当于 。请参阅此处与此相关的 XML 规范。

如果您仍然需要,那么您就做错了

As far as XML is concerned, <player/> is equivalent to <player></player>. See XML spec here related to this.

If you still need to have <player></player> then you are doing something wrong.

£冰雨忧蓝° 2025-01-04 11:37:19

我假设你的问题是当你读取一个空节点时你会崩溃。在尝试读取任何元素/属性之前,您应该始终检查是否有空元素。

I am assuming your problem is when you read an empty node you are crashing. You should always check for empty elements before trying to read any elements/attributes.

画离情绘悲伤 2025-01-04 11:37:19

它们应被视为等效值。但是,如果您需要,那么使用自定义 XmlWriter 可能是您最好的选择,如此处类似问题的答案中所述:

http://social.msdn.microsoft.com/Forums/en-US/xmlandnetfx/thread/979315cf-6727-4979-a554-316218ab8b24/

They should be considered equivalent values. However, if you need then working with a custom XmlWriter may be your best bet, as described here in the answer to a similar question here:

http://social.msdn.microsoft.com/Forums/en-US/xmlandnetfx/thread/979315cf-6727-4979-a554-316218ab8b24/

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