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
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;
The 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>
I have checked the variable name and it correctly prints out "sampleapp" and my XPath is correct.
EDIT:
It's still in a non-working condition, but I got it to not have a null reference error but now it has an XPath error that I don't compeltely understand because the variables fine and it prints out just fine, and the path is fine because I tested it with multiple XPath tester utiltiiees
Not really that different but slightly changed code for removing node
try
{
Console.WriteLine(uninstallernodename);
string packagepath = "/packages/installed/";
XmlNode PackagesListForRemoval = InstalledList.SelectSingleNode(packagepath + nodename2bdeleted);
PackagesListForRemoval.ParentNode.RemoveChild(PackagesListForRemoval);
InstalledList.Save("Apps.installed");
}
catch (Exception Failed2DelNode)
{
Console.WriteLine("Cleanup failure, please ignore the message below unless you are a software developer, the problem should not affect you but you may always go online for support");
Console.WriteLine(Failed2DelNode);
ErrorDumpWriter.WriteLine(" ");
ErrorDumpWriter.WriteLine(DateTime.Now);
ErrorDumpWriter.WriteLine(Failed2DelNode);
}
Code for the other parts that are missing that are neccessary:
XmlNodeList PackagesNode = InstalledList.SelectNodes(installedpkgxpath + "*");
string uninstallernodepkgid = null;
string uninstallernodename = null;
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;
Console.WriteLine(uninstallernodename);
Just know that as you can tell, it's not the full code just the parts that are needed
Also the full output of the program is
tallernodename);