The program I am working on is having a null reference exception whenever it tries to delete the node.

The buggy code:

First one is where the variable is declared for getting the name of the node to be deleted, just node htese are bits of the code

foreach (XmlNode InstalledListNodes in PackagesNode)
				{

					//If the title is the same as what the user typed, continue on
					if (InstalledListNodes.Attributes["title"].Value.Equals(packagename) == true)
					{
						uninstallernodename = InstalledListNodes.LocalName;

Second one is where it is deleted

XmlNode PackagesListForRemoval = InstalledList.SelectSingleNode("/packages/installed/" + uninstallernodename);
						PackagesListForRemoval.ParentNode.RemoveChild(PackagesListForRemoval);
						InstalledList.Save("Apps.installed");

The XML File

<?xml version="1.0" encoding="utf-8" ?>
<packages>
  <installed>
  <sampleapp title="sampleapp" id="00001" unintallername="sampleapp.bat" installdate="11/15/09"></sampleapp>
  <sampleapp2 title="sampleapp2" id="00002" uninstallername="sampleapp2.bat" installdate="11/16/09"></sampleapp2>
  </installed>
  <uninstalled>

 </uninstalled>
</packages>

Take a look at following code,

..
        var doc = XDocument.Load(@"sample1.xml");
        var t = from node in doc.Descendants("installed")  select node;
        
        var m = from node1 in t.Descendants() where node1.Attribute("title").Value == "sampleapp" select node1;
        if (m.ToList().Count!=0)
        {
            m.Remove();
            doc.Save(@"sample1.xml");
        }
..
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.