XPath的问题

ceaseriii 2003-09-12 03:36:41
有一个xml文件如下所示:
<bookstore xmlns="schema.xsd">
<book genre="autobiography" publicationdate="1981" ISBN="1-861003-11-0">
<title>The Autobiography of Benjamin Franklin</title>
</book>
<book genre="novel" publicationdate="1967" ISBN="0-201-63361-2">
<title>The Confidence Man</title>
</book>
<book genre="philosophy" publicationdate="1991" ISBN="1-861001-57-6">
<title>The Gorgias</title>
</book>
</bookstore>

以下是C#的代码:
XPathNodeIterator myXPathNodeIterator = myXPathNavigator.Select("/bookstore/book/title");
while(myXPathNodeIterator.MoveNext())
{
Console.WriteLine("<" + myXPathNodeIterator.Current.Name + "> " + myXPathNodeIterator.Current.Value);
}
我不能得到title的值,这是为什么?
...全文
27 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
ceaseriii 2003-09-12
  • 打赏
  • 举报
回复
解决了,因为涉及到名称空间,所以问题变得没有那么简单,改变后的代码如下
XmlNamespaceManager nsMgr = new XmlNamespaceManager(myDoc.NameTable);
nsMgr.AddNamespace("p", "schema.xsd");
// p 是任意给定的前缀,schema.xsd是我的xml文件名称空间的名字
XPathExpression myXPathExpr;
myXPathExpr = myXPathNavigator.Compile(/p:bookstore/p:book/p:title);
myXPathExpr.SetContext(nsMgr);

XPathNodeIterator myXPathNodeIterator = myXPathNavigator.Select(myXPathExpr);
ceaseriii 2003-09-12
  • 打赏
  • 举报
回复
[Current]后面没有[InnerText]这个属性呀。
freeboy0002 2003-09-12
  • 打赏
  • 举报
回复

Console.WriteLine("<" + myXPathNodeIterator.Current.Name + "> " + myXPathNodeIterator.Current.InnerText);

12,163

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 Web Services
社区管理员
  • Web Services社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧