你的第二个和第三个XPath表达式结果是布尔值,你也许需要这样:
(//my:ele5[@type='complex']/@type)|(//my:ele5[@type='simple']/@type)

解决方案 »

  1.   


    我改了程序,现在是这样:        static void Main(string[] args)
            {
                var doc = new XmlDocument();
                doc.Load(@"d:\my\my.xml");
                var root = doc.DocumentElement;
                var nsmgr = new XmlNamespaceManager(doc.NameTable);
                nsmgr.AddNamespace("my", "http://m.com");
                var list = root.SelectNodes("//my:ele5",nsmgr);
                foreach (XmlElement n in root)
                {
                    var acom = n.SelectNodes("//my:ele5[@type='complex']/@type)|(//my:ele5[@type='simple']/@type)");
                    foreach (XmlAttribute a in acom)
                        Console.WriteLine(a);
                }
            }
    但是运行时SelectNodes函数抛异常:
    -----------------------------------------------------
    未经处理的异常:  System.Xml.XPath.XPathException: “//my:ele5[@type='complex']/@type)|(//my:ele5[@type='simple']/@type)”具有无效的标记。
       在 MS.Internal.Xml.XPath.XPathParser.ParseXPathExpresion(String xpathExpresion)
       在 System.Xml.XPath.XPathExpression.Compile(String xpath, IXmlNamespaceResolver nsResolver)
       在 System.Xml.XPath.XPathNavigator.Select(String xpath)
       在 System.Xml.XmlNode.SelectNodes(String xpath)
       在 UseCom.Program.Main(String[] args) 还要怎么改呢?
      

  2.   


    啊,是的,我把缺少的括号加上了,像下面这样:    static void Main(string[] args)
        {
            var doc = new XmlDocument();
            doc.Load(@"d:\my\my.xml");
            var root = doc.DocumentElement;
            var nsmgr = new XmlNamespaceManager(doc.NameTable);
            nsmgr.AddNamespace("my", "http://m.com");
            //var list = root.SelectNodes("//my:ele5", nsmgr);
            foreach (XmlElement n in root)
            {
                var acom = n.SelectNodes("(//my:ele5[@type='complex']/@type)|(//my:ele5[@type='simple']/@type)");
                foreach (XmlAttribute a in acom)
                    Console.WriteLine(a);
            }
        }但是现在还是有异常抛出:未经处理的异常:  System.Xml.XPath.XPathException: 需要命名空间管理器或 XsltContext。此查询具有前缀、变量或用户定义的函数。
       在 MS.Internal.Xml.XPath.CompiledXpathExpr.get_QueryTree()
       在 System.Xml.XPath.XPathNavigator.Evaluate(XPathExpression expr, XPathNodeIterator context)
       在 System.Xml.XPath.XPathNavigator.Evaluate(XPathExpression expr)
       在 System.Xml.XPath.XPathNavigator.Select(XPathExpression expr)
       在 System.Xml.XPath.XPathNavigator.Select(String xpath)
       在 System.Xml.XmlNode.SelectNodes(String xpath)
       在 UseCom.Main(String[] args)