質問

HTML Agilityパックを使用して、特定のIDを持つdivとその子を削除しようとするのが困難です。設定オプションが不足していると確信していますが、金曜日であり、苦労しています。

簡易HTMLの実行:

<html><head></head><body><div id='wrapper'><div id='functionBar'><div id='search'></div></div></div></body></html>

これは私の知る限りです。アジリティパックによってスローされるエラーは、div構造が見つからないことを示しています。

<div id='functionBar'></div>

これまでのコードは次のとおりです(Stackoverflow ....から取得)

HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument();
        // There are various options, set as needed
        //htmlDoc.OptionFixNestedTags = true;

        // filePath is a path to a file containing the html
        htmlDoc.LoadHtml(Html);

        string output = string.Empty;

        // ParseErrors is an ArrayList containing any errors from the Load statement
        if (htmlDoc.ParseErrors != null && htmlDoc.ParseErrors.Count > 0)
        {
            // Handle any parse errors as required

        }
        else
        {

            if (htmlDoc.DocumentNode != null)
            {
               HtmlAgilityPack.HtmlNode bodyNode  = htmlDoc.DocumentNode.SelectSingleNode("//body");

                if (bodyNode != null)
                {
                    HtmlAgilityPack.HtmlNode functionBarNode = bodyNode.SelectSingleNode ("//div[@id='functionBar']");

                    bodyNode.RemoveChild(functionBarNode,false);

                    output = bodyNode.InnerHtml;
                }
            }
        }
役に立ちましたか?

解決

  

bodyNode.RemoveChild(functionBarNode、false);

ただし、functionBarNodeはbodyNodeの子ではありません。

functionBarNode.ParentNode.RemoveChild(functionBarNode、false)はどうですか? (そして、bodyNodeを見つけることについて少し忘れてください。)

他のヒント

次の電話をかけることができます:

var documentNode = document.DocumentNode;
var functionBarNode = documentNode.SelectSingleNode("//div[@id='functionBar']");
functionBarNode.Remove();

これははるかに単純で、次と同じです:

functionBarNode.ParentNode.RemoveChild(functionBarNode, false);

これは複数で機能します:

HtmlDocument d = this.Download(string.Format(validatorUrl, Url));
foreach (var toGo in QuerySelectorAll(d.DocumentNode, "p[class=helpwanted]").ToList())
{
   toGo.Remove();
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top