Search code examples
c#xmlnullreferenceexceptionxpathselectsinglenode

NullReferenceException when appending attribute to the element in xml using c#


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>

Solution

  • 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);