使用 XpathNavigator 和 HtmlAgilityPack 查找 InnerHtml 值
test.xml 的一部分
<tr class="a">
<td align="left" nowrap="true">desc1</td>
<td align="left">desc2</td>
<td>desc3</td>
<td align="left">desc4</td>
<td align="left">desc5</td>
<td>desc6</td>
<td>desc7</td>
<td>desc8</td>
<td class="nr">desc9</td>
</tr>
//create XpathNavigator to get the last value inside td ie desc9
> HtmlDocument document = new HtmlDocument();
document.Load(Server.MapPath("test.xml"));
XPathNavigator xPathNavigator = document.CreateNavigator();
object o = xPathNavigator.Evaluate("/table[1]/tbody[1]/tr[2]/td[9]");
调试器显示可以按如下方式计算该值,这非常麻烦。
((HtmlAgilityPack.HtmlNodeNavigator)((new System.Linq.SystemCore_EnumerableDebugView(((MS.Internal.Xml.XPath.XPathSelectionIterator)(o)))).Items[0])).Value
前往 desc9 的最佳方式是什么?
portion of test.xml
<tr class="a">
<td align="left" nowrap="true">desc1</td>
<td align="left">desc2</td>
<td>desc3</td>
<td align="left">desc4</td>
<td align="left">desc5</td>
<td>desc6</td>
<td>desc7</td>
<td>desc8</td>
<td class="nr">desc9</td>
</tr>
//create XpathNavigator to get the last value inside td i.e. desc9
> HtmlDocument document = new HtmlDocument();
document.Load(Server.MapPath("test.xml"));
XPathNavigator xPathNavigator = document.CreateNavigator();
object o = xPathNavigator.Evaluate("/table[1]/tbody[1]/tr[2]/td[9]");
The debugger shows the value can be evaluated as below which is very cumbersome.
((HtmlAgilityPack.HtmlNodeNavigator)((new System.Linq.SystemCore_EnumerableDebugView(((MS.Internal.Xml.XPath.XPathSelectionIterator)(o)))).Items[0])).Value
What is the best way to get to desc9?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我没有使用过 XPathNavigator,但这里有一个类似的解决方案,具有 SelectNodes/SelectSingleNode 样式和 HTML Agility Pack。
仅供
参考 - 关于 HTML Agility 包的最佳文档似乎是源代码中包含的示例。不知道为什么这不是文档中的单独下载。
I haven't used the XPathNavigator but here is a similar solution with the SelectNodes/SelectSingleNode style and the HTML Agility Pack.
OR
FYI - The best documentation on the HTML Agility pack seems to be the samples included with the Source. Not sure why that isn't a separate download in the documentation.
像这样的东西:
看看 XPath 语法
something like this:
Take a look at XPath Syntax
我认为你的做法是错误的。
我相信您需要做的只是以下内容:
我找不到文档的在线副本来链接您,但您可以检查 http://htmlagilitypack.codeplex.com/releases/view/44954 了解更多详细信息。
另外,如果您只是阅读 XML,那么您使用 html 敏捷包是否有任何原因,或者只是您的测试文件是有效的 XML?
I think you are going about this wrong.
I believe all you should need to do is something along the lines of:
I can't find an online copy of the docs to link you to but you can check the docs found at http://htmlagilitypack.codeplex.com/releases/view/44954 for more details.
Also if you are just reading XML is there any reason why you are using the html agility pack or is it just your test file that is valid XML?