将对象序列化为 XML 元素 (c#)
[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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
就 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.我假设你的问题是当你读取一个空节点时你会崩溃。在尝试读取任何元素/属性之前,您应该始终检查是否有空元素。
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.
它们应被视为等效值。但是,如果您需要,那么使用自定义 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/