使用 C# 将属性附加到 xml 中的元素时出现 NullReferenceException

发布于 2024-10-06 05:46:59 字数 1548 浏览 6 评论 0原文

当尝试将元素附加到其父元素时,我收到 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 技术交流群。

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

发布评论

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

评论(2

对你的占有欲 2024-10-13 05:46:59

SelectSingleNode 过程将返回 null,因为尚未找到 gameId:

dartDatabase.DocumentElement.SelectSingleNode("/games/game[@id='gameId']");

因此 existingGamenull,因此 >调用时抛出 NullReferenceException

existingGame.AppendChild(newPlayer);

您必须像这样转义 gameId:

SelectSingleNode("/games/game[@id='" + gameId.ToString() + "']");

一种更简单的属性方法,XmlElement 更具体。
但你不应该尝试不去更一般化,除非你不能用 XmlElement 做到这一点......

var existingGame = (XmlElement) doc.DocumentElement.SelectSingleNode("...");

existingGame.SetAttribute("id", gameId);

The SelectSingleNode procedure will return null as gameId has not been found:

dartDatabase.DocumentElement.SelectSingleNode("/games/game[@id='gameId']");

Hence existingGame is null and thus the NullReferenceException is thrown when calling:

existingGame.AppendChild(newPlayer);

You will have to escape the gameId like this:

SelectSingleNode("/games/game[@id='" + gameId.ToString() + "']");

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...

var existingGame = (XmlElement) doc.DocumentElement.SelectSingleNode("...");

existingGame.SetAttribute("id", gameId);
画骨成沙 2024-10-13 05:46:59

你检查过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.)

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