求一个XPath表达式 用XmlDocument.SelectNodes("//bookstore"); 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 用"//bookstore"只能选根结点,可是我现在需要要全部结点。我又试了一下"//bookstore//",又报这个异常了。真是急死了。 参考如下代码,我测试使用的是有声明命名空间的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); } } java签名转C# debug 与 release 两个一维数组取差的问题 如何修改登陆SQL2005数据库的密码和帐号? 注册表关联协议,是如何发送消息的,程序又是如何接受消息的? winforms中有这样的控件吗 !100分求解(不够再加) 关于Tif格式转换为Jpg的问题~~ 急在线求解 c#设置系统时间方法 关于 同时向磁盘写入多个文件 从存储过程中返回参数的问题 请问 如何引用一个自定义.net组件(From dll )而不需要拷贝到相同的目录 DataGridView 取数据的问题?
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);
}
}