Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3458288
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T09:57:52+00:00 2026-05-18T09:57:52+00:00

I get a NullReferenceException when trying to append element to its parent to element,

  • 0

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>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T09:57:52+00:00Added an answer on May 18, 2026 at 9:57 am

    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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to add ConfirmDialogHandler to dialog handler and i get NullReferenceException. This happens
So, I get this System.NullReferenceException Object reference not set to an instance of an
I have this XML file that I parse into its elements and create a
Get sunrise time and sun set times from xml from web. This is the
I'm new to IoC and I'm trying to get started using StructureMap, but it
am trying to get the parameter id from the query string of the requested
I get a NullReferenceException when my target provider is SQL Server CE, but with
//Get width and resize another element $(document).ready(function() { function ResizeSearch(GridID, SearchID) { var eleWidth
I am getting NullReferenceException error on _attr.Append(xmlNode.Attributes[name]);. namespace SMAS { class Profiles { private
I'm trying to get a reference to the user object in my Global.asax file's

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.