2009-09-18 4 views
1

HTML 민첩성 팩을 사용하여 특정 ID와 그 하위가있는 div를 제거하는 데 문제가 있습니다. 나는 단지 설정 옵션이 빠졌을 것이라고 확신하지만, 금요일과 나는 고군분투하고있다.html 민첩성 팩 어린이 제거

단순화 된 HTML 실행 : 이것은 지금까지 내가 가지고 같다

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

. 민첩성 팩에 의해 발생 된 오류는이 사업부 구조를 찾을 수 없습니다 보여줍니다 (.... 유래에서 촬영) 여기

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

지금까지 코드의를

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; 
       } 
      } 
     } 

답변

7

bodyNode.RemoveChild (functionBarNode ,그릇된);

그러나 functionBarNode는 bodyNode의 하위 노드가 아닙니다.

어때 대략 functionBarNode.ParentNode.RemoveChild(functionBarNode, false)?

+0

천재 - 감사합니다! –

3

당신은 간단하게 호출 할 수 있습니다 (. 그리고 bodyNode 찾기에 대한 비트를 잊지) :

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

그것은 훨씬 더 간단하고, 같은 수행합니다 여러 작동합니다

functionBarNode.ParentNode.RemoveChild(functionBarNode, false); 
0

이를 :

HtmlDocument d = this.Download(string.Format(validatorUrl, Url)); 
foreach (var toGo in QuerySelectorAll(d.DocumentNode, "p[class=helpwanted]").ToList()) 
{ 
    toGo.Remove(); 
} 
관련 문제