使用 C# 将属性附加到 xml 中的元素时出现 NullReferenceException
当尝试将元素附加到其父元素时,我收到 NullReferenceException,如下所示
XmlDocument dartDatabase = new XmlDocument();
string path = @"D:\xml\dartDatabase.xml";
XPath
dartDatabase.Load(path);
XmlNode newGame = dartDatabase["games"].AppendChild(dartDatabase.CreateElement("game"));
XmlNode newGameId = dartDatabase.CreateAttribute("id");
newGameId.Value = gameId.ToString();
newGame.Attributes.SetNamedItem(newGameId);
......
XmlNode existingGame = dartDatabase.DocumentElement.SelectSingleNode("/games/game[@id='gameId']");
XmlNode newPlayer = dartDatabase.CreateElement("player");
existingGame.AppendChild(newPlayer);
//dartDatabase.DocumentElement.InsertAfter(newPlayer, dartDatabase.DocumentElement.LastChild);
XmlNode newPlayerId = dartDatabase.CreateAttribute("id");
newPlayerId.Value = playerId.ToString();
newPlayerId.Attributes.SetNamedItem(newPlayer);
表达式中可能存在一些错误,因此我尝试将元素 newPlayer 作为 lastChild 附加,只是为了尝试后面的代码。当将属性 newPlayerId 附加到元素 newElement 时,我也得到 NullReferenceException 。第一部分工作得很好,它创建像这样的 xml 文件,
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<games>
<game id="2" name="501" />
</games>
但第二部分,当我尝试将 newPlayer 元素添加到我通过属性 id 确定的特定元素时。
xml 文件应该如下所示
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<games>
<game id="2" name="501">
<player id="1">
</player>
</game>
</games>
I get a NullReferenceException when trying to append element to its parent to element, like this
XmlDocument dartDatabase = new XmlDocument();
string path = @"D:\xml\dartDatabase.xml";
...
dartDatabase.Load(path);
XmlNode newGame = dartDatabase["games"].AppendChild(dartDatabase.CreateElement("game"));
XmlNode newGameId = dartDatabase.CreateAttribute("id");
newGameId.Value = gameId.ToString();
newGame.Attributes.SetNamedItem(newGameId);
...
XmlNode existingGame = dartDatabase.DocumentElement.SelectSingleNode("/games/game[@id='gameId']");
XmlNode newPlayer = dartDatabase.CreateElement("player");
existingGame.AppendChild(newPlayer);
//dartDatabase.DocumentElement.InsertAfter(newPlayer, dartDatabase.DocumentElement.LastChild);
XmlNode newPlayerId = dartDatabase.CreateAttribute("id");
newPlayerId.Value = playerId.ToString();
newPlayerId.Attributes.SetNamedItem(newPlayer);
There is probably some error in XPath expression, so i tried to append element newPlayer as a lastChild, just to try code that follows. And there too i get NullReferenceException when appending attribute newPlayerId to element newElement. First part works just fine, it create xml file like this,
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<games>
<game id="2" name="501" />
</games>
but second part, when i try to add newPlayer element to a specific element which i determine by attribute id.
xml file should look like this
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<games>
<game id="2" name="501">
<player id="1">
</player>
</game>
</games>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
SelectSingleNode
过程将返回null
,因为尚未找到 gameId:因此
existingGame
为null
,因此>调用时抛出 NullReferenceException
:您必须像这样转义 gameId:
一种更简单的属性方法,XmlElement 更具体。
但你不应该尝试不去更一般化,除非你不能用 XmlElement 做到这一点......
The
SelectSingleNode
procedure will returnnull
as gameId has not been found:Hence
existingGame
isnull
and thus theNullReferenceException
is thrown when calling:You will have to escape the gameId like this:
A much easier approach to attributes, XmlElement is more specific.
But you shouldn't try not to go more general unless you can't do it with XmlElement...
你检查过existingGame和newPlayer都是非空的吗?我猜测
SelectSingleNode
没有找到任何内容并返回 null。 (我认为 newPlayer 不可能为空,但请检查以防万一。)Did you check that existingGame and newPlayer are both non-null? I'm guessing that
SelectSingleNode
didn't find anything and returned null. (I don't think it's possible for newPlayer to be null, but check just in case.)