用XmlDocument.SelectNodes("//bookstore");

解决方案 »

  1.   

    用"//bookstore"只能选根结点,可是我现在需要要全部结点。我又试了一下"//bookstore//",又报这个异常了。真是急死了。
      

  2.   

    参考如下代码,我测试使用的是有声明命名空间的xml文件,所以有query.SetContext(manager)这一行代码。            XPathDocument document = new XPathDocument("books.xml");
                XPathNavigator navigator = document.CreateNavigator();
                XmlNamespaceManager manager = new XmlNamespaceManager(navigator.NameTable);
                manager.AddNamespace("bk", "http://www.contoso.com/books");
                XPathExpression query = navigator.Compile("//bk:bookstore");
                query.SetContext(manager);
                object oResult = navigator.Evaluate(query);
                if (query.ReturnType == XPathResultType.NodeSet)
                {
                    XPathNodeIterator nodesText = (XPathNodeIterator)oResult;
                    while (nodesText.MoveNext())
                    {
                        Console.WriteLine(nodesText.Current.Name);
                        Console.WriteLine(nodesText.Current.Value);
                    }
                }