使用 agilityhtml 获取 div 的特定部分的值

Get the value of a specific part of a div using agilityhtml

我正在尝试使用 agilitypack.my html 获取 div 的值,代码如下:

<div class="div_5">
                <p>First Paragraph</p>
                <p>Second Paragraph</p>
                <p>Third Paragraph</p>
                <p>Fourth Paragraph</p>

<div class="div_6">
                <p>First Paragraph</p>
                <p>Second Paragraph</p>
                <p>Third Paragraph</p>
                <p>Fourth Paragraph</p>
     </div>
                <p>other Paragraph</p>
                <p>other Paragraph</p>
  </div>

我需要 div_5 的内容而不需要 div_6 的内容,所以我使用此代码:

    newsContent.Content = resultat1.DocumentNode.SelectSingleNode("//div[@class='div_5']").InnerHtml;

但此代码包含 div_5div_6。我如何从我的值中删除 div_6

我从未使用过 AgilityHTML,但可以尝试以下方法:

var div5 = resultat1.DocumentNode.SelectSingleNode("//div[@class='div_5']");

var div6 = div5.DocumentNode.SelectSingleNode("//div[@class='div_6']");

div6.Remove();

newsContent.Content = div5.InnerHtml;

先删除innernode,然后再继续。

var yourNode = resultat1.DocumentNode.SelectSingleNode("//div[@class='div_5']")
var toBeRemoved = resultat1.DocumentNode.SelectSingleNode ("//div[@class='_div_6']");

yourNode.RemoveChild(toBeRemoved,false);
//proceed with your code
newsContent.Content = yourNode.InnerHtml;  

最终代码:

HtmlNode doc = resultat1.DocumentNode.SelectSingleNode("//div[@class='div_5']");
                    HtmlNode node = doc.SelectSingleNode("//div[@class='div_6']");
                    node.ParentNode.RemoveChild(node);