简体   繁体   English

XPath Expression必须使用OK XPath Path评估为节点集(已通过在线XPath测试仪和Oxygen测试)

[英]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. 每当我尝试删除该节点时,我正在处理的程序都会具有null引用异常。

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文件

<?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. 我已经检查了变量名,它正确地打印出了“ sampleapp”,并且我的XPath是正确的。

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 它仍然处于无法正常工作的状态,但是我得到它没有空引用错误,但是现在它有一个XPath错误,我无法完全理解,因为变量很好,并且可以很好地打印出来,并且路径也很好因为我使用多个XPath测试器实用程序对其进行了测试

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 程序的完整输出也是 http://i48.tinypic.com/dwtlxd.png

tallernodename); tallernodename);

Why don't you change your xpath to this: 为什么不将xpath更改为此:

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: 编辑:如果您不确定节点将是什么类型,则对xpath的简单更改将解决以下问题:

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. 我刚刚使用您提供的xml对此进行了测试,并且工作正常。 If it doesn't then you are doing something else wrong. 如果没有,那么您正在做其他错误。

If you need more help on XML and XPath, check this link . 如果您需要有关XML和XPath的更多帮助, 请检查此链接

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 XSLT XPATH 错误(表达式必须计算为节点集) - Error with XSLT XPATH (Expression must evaluate to a node-set) Xpath错误:表达式必须计算为节点集 - Xpath Error: Expression must evaluate to a node-set xpath表达式count()失败,异常,Expression必须求值为一个节点集 - xpath expression count() fails with exception, Expression must evaluate to a node-set xml错误:System.Xml.XPath.XPathException:表达式必须计算为节点集 - xml error:System.Xml.XPath.XPathException: Expression must evaluate to a node-set System.Xml.XPath.XPathException:执行 SelectSingleNode(“//(artist|author)”) 时,表达式必须计算为节点集 - System.Xml.XPath.XPathException: Expression must evaluate to a node-set when executing SelectSingleNode(“//(artist|author)”) 未处理的异常:System.Xml.XPath.XPathException:Expression必须求值为node-set - Unhandled Exception: System.Xml.XPath.XPathException: Expression must evaluate t o a node-set XPath问题,出现“表达式必须计算为节点集”的错误。 - XPath problem, getting “expression must evaluate to a node-set.” error 表达式在获取值时必须求值到Xpath错误中设置的节点 - Expression must evaluate to node set in Xpath error while trying to get value XsltTransform System.Xml.XPath.XPathException: &#39;表达式必须计算为节点集。&#39; - XsltTransform System.Xml.XPath.XPathException: 'Expression must evaluate to a node-set.' XPathSelectElements始终返回Expression必须计算为节点集 - XPathSelectElements always returns Expression must evaluate to a node-set
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM