XPath表达式必须使用OK XPath Path评估节点集(使用在线XPath测试器和Oxygen测试)

时间:2009-11-19 01:52:25

标签: c# xml xpath

我正在处理的程序在尝试删除节点时都有一个空引用异常。

有缺陷的代码:

第一个是声明变量以获取要删除的节点名称的位置

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;

第二个是删除的地方

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

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>

我检查了变量名称,它正确打印出“sampleapp”,我的XPath是正确的。

编辑:

它仍然处于非工作状态,但我得到它没有空引用错误但现在它有一个XPath错误,我不能完全理解,因为变量很好,它打印出来就好了,并且路径很好,因为我用多个XPath测试器工具测试了它

用于删除节点

的代码并不是那么不同但略有改变
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);
                    }

缺少必要的其他部分的代码:

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);

只要知道,正如你所知道的那样,完整的代码不仅仅是需要的部分

该程序的完整输出也是 http://i48.tinypic.com/dwtlxd.png

tallernodename);

1 个答案:

答案 0 :(得分:1)

为什么不将xpath更改为:

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

编辑: 如果您不确定该节点的类型,那么对xpath的简单更改将修复:

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

这将选择具有替换标题的所有子节点,而不管其类型如何。如果您使用更简单的代码行来执行此操作:

        XmlNode node = doc.SelectSingleNode(xpath);

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

我刚刚使用您提供的xml对此进行了测试,并且运行正常。如果没有,那你就是做错了。

如果您需要有关XML和XPath的更多帮助,check this link