문제

나는 특정 ID로 DIV를 제거하는 데 어려움을 겪고 있으며 HTML 민첩성 팩을 사용하여 어린이들을 사용합니다. 나는 단지 구성 옵션이 없다고 확신하지만 금요일과 나는 고군분투하고 있습니다.

단순화 된 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는 바디 노드의 자녀가 아닙니다.

어때 functionBarNode.ParentNode.RemoveChild(functionBarNode, false)? (그리고 바디 노드를 찾는 것에 대한 비트를 잊어 버리십시오.)

다른 팁

간단히 전화 할 수 있습니다.

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