本文介绍了如何使用html agility pack和xpath获取所有子节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我有一个XML文档,我试图使用XPath解析HTML Agility Pack。这是我获取元素的代码 HtmlDocument htmldoc = new HtmlDocument(); htmldoc.Load(fileName); HtmlNodeCollection nodes = htmldoc.DocumentNode.SelectNodes( feed / l); 这是我的XML文档 <? xml version = 1.0 编码 = UTF-8 ? > < feed > < tm > 5701 < / tm > < tp > 1141 < / tp > < pnr > 1 < / pnr > < l type = TEXT > < ca > < c id = 21 > JA < / c > ; < / ca > < pts > < pt id = 5 > DO < / pt > < / pts > < od > MA 99 < / od > < osd > 2014-02-21 < / osd > < od > 2014-02-22 < / od > < cu > url < / cu > < ip > ip < / ip > < ai > 3184 < / ai > < > com < / an > < n id = 1 > US n < / n > < / l > < l type = TEXT > < ca > < c id = 21 > JA < / c > < / ca > < pts > < pt id = 5 > DO < / pt > < / pts > < od > MA 99 < / od > < osd > 2014-02-21 < / osd > < od > 2014-02-22 < / od > < cu > url < / cu > < ip > ip < / ip > < ai > 3184 < / ai > < > com < / an > < n id = 1 > US n &升t; / n > < / l > < l type = TEXT > < ca > < c id = 21 > JA < / c > < / ca > < pts > ; < pt id = 5 > DO < / pt > < / pts > < od > MA 99 < / od > < osd > 2014-02-21 < / osd > < od > 2014-02-22 < / od > < cu > url < / cu > < ip > ip < / ip > < ai > 3184 < / ai > < > com < / an > < n id = 1 > US n < / n > < / l > < l type = TEXT > < ca > < c id = 21 > JA < ; / c > < / ca > < pts > < pt id = 5 > DO < ; / pt > < / pts > < od > MA 99 < / od > < osd > 2014-02-21 < / osd > < ; od > 2014-02-22 < / od > < cu > url < / cu > < ip > ip < / ip > < ai > 3184 < / ai > < > com < / an > < n id = 1 > black US n < / n > < / l > < / feed > 我试图得到所有l的子节点及其子节点,但我的xpath将节点带回l作为集合,但子节点为空。我正在寻找的节点按l是这些 < ca > < c id = 21 > JA < / c > < / ca > < pts > < pt id = 5 > DO < / pt > < / pts > < od > MA 99 < / od > < osd > 2014-02-21 < / osd > < od > 2014-02-22 < / od > < cu > url < / cu > < ip > ip < / ip > < ai > 3184 < / ai > < > com < / an > < n id = 1 > black US n < / n > 但是没有孩子。请帮助我的xpath似乎是正确的解决方案 尝试使用 htmldoc.DocumentNode.SelectNodes(// l); I have an XML document i am trying to parse with HTML Agility Pack using XPath. This is my code for getting to the elementsHtmlDocument htmldoc = new HtmlDocument();htmldoc.Load(fileName);HtmlNodeCollection nodes = htmldoc.DocumentNode.SelectNodes("feed/l");This is my XML document<?xml version="1.0" encoding="UTF-8"?><feed><tm>5701</tm><tp>1141</tp><pnr>1</pnr><l type="TEXT"><ca><c id="21">JA</c></ca><pts><pt id="5">DO</pt></pts><od>MA 99</od><osd>2014-02-21</osd><od>2014-02-22</od><cu>url</cu><ip>ip</ip><ai>3184</ai><an>com</an><n id="1">US n</n></l><l type="TEXT"><ca><c id="21">JA</c></ca><pts><pt id="5">DO</pt></pts><od>MA 99</od><osd>2014-02-21</osd><od>2014-02-22</od><cu>url</cu><ip>ip</ip><ai>3184</ai><an>com</an><n id="1">US n</n></l><l type="TEXT"><ca><c id="21">JA</c></ca><pts><pt id="5">DO</pt></pts><od>MA 99</od><osd>2014-02-21</osd><od>2014-02-22</od><cu>url</cu><ip>ip</ip><ai>3184</ai><an>com</an><n id="1">US n</n></l><l type="TEXT"><ca><c id="21">JA</c></ca><pts><pt id="5">DO</pt></pts><od>MA 99</od><osd>2014-02-21</osd><od>2014-02-22</od><cu>url</cu><ip>ip</ip><ai>3184</ai><an>com</an><n id="1">black US n </n></l></feed>I am trying to get all the node that are children of "l" and their children but my xpath brings nodes back with "l" as a collection but the children are null. the nodes i am looking for grouped by "l are these<ca><c id="21">JA</c></ca><pts><pt id="5">DO</pt></pts><od>MA 99</od><osd>2014-02-21</osd><od>2014-02-22</od><cu>url</cu><ip>ip</ip><ai>3184</ai><an>com</an><n id="1">black US n </n>but there are no children. Please help my xpath seems to be correct 解决方案 Try using htmldoc.DocumentNode.SelectNodes("//l"); 这篇关于如何使用html agility pack和xpath获取所有子节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-24 11:34