开发者

XPath Expression must evaluate to a node-set with OK XPath Path (Tested with online XPath tester and Oxygen)

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 co开发者_StackOverflow社区rrectly 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

XPath Expression must evaluate to a node-set with OK XPath Path (Tested with online XPath tester and Oxygen)

tallernodename);


Why don't you change your xpath to this:

InstalledList.SelectSingleNode(string.Format("/packages/installed/sampleapp [@title='{0}']", uninstallernodename));

EDIT: If you are not sure what type the node will be, then a simple change to the xpath will fix that:

InstalledList.SelectSingleNode(string.Format("/packages/installed/* [@title='{0}']", uninstallernodename));

This selects all child nodes that have the title that is substituted in, regardless of type. If you follow this with a couple more simple lines of code:

        XmlNode node = doc.SelectSingleNode(xpath);

        XmlNode parent = node.ParentNode;
        parent.RemoveChild(node);

I have just ran a test on this, using the xml you supplied, and it worked fine. If it doesn't then you are doing something else wrong.

If you need more help on XML and XPath, check this link.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜