XPath能否只包括属性的名字而没有元素的名字? 你的第二个和第三个XPath表达式结果是布尔值,你也许需要这样:(//my:ele5[@type='complex']/@type)|(//my:ele5[@type='simple']/@type) 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我改了程序,现在是这样: 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) 还要怎么改呢? 啊,是的,我把缺少的括号加上了,像下面这样: 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) 求通用的SaveModel方法 有没有C#写的类似lex、yacc的工具啊? 求教list<类> 怎么在循环中删除多个满足条件的类 数组的比较(C#)来着有分 C#调用 C++DLL 含有回调函数 c#的编译问题 各位大侠帮帮忙,急!!!!!! 如何查找DATAGRID页脚中的DROPDOWNLIST控件 WinForm问题,高手请进>> datagrid中的分页排序,高手指点 XPath是否支持字符串通配? 求助c#打字软件的实现,谢谢!
我改了程序,现在是这样: 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) 还要怎么改呢?
啊,是的,我把缺少的括号加上了,像下面这样: 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)