0

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>
2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by __avd
0

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");
        }
..
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.