私は特定のIDを持つdivとHTML Agilityパックを使用するその子を削除しようとするのが難しいです。私は設定オプションが不足していると確信していますが、金曜日と私は苦労しています。
単純化された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);