2013-12-22 11 views
1

HTML은HTML 민첩성 구문 분석 오류

<html> 
<head> 
<title>Sample Page</title> 
</head> 
<body> 
<form action="demo_form.asp" id="form1" method="get"> 
    First name: <input type="text" name="fname"><br> 
    Last name: <input type="text" name="lname"><br> 
    <input type="submit" value="Submit"> 
</form> 
</body> 
</html> 

코드

HtmlDocument doc = new HtmlDocument();  
doc.LoadHtml(File.ReadAllText(@"C:\sample.html")); 
HtmlNode nd = doc.DocumentNode.SelectSingleNode("//form[@id='form1']"); 
//nd.InnerHtml is "". 
//nd.InnerText is "". 

문제 내가 원하는 위해 하나에 의해 ID = Form1에 하나 양식 태그의 childNodes에 액세스 할 수

nd.ChildNodes //Collection(to get all nodes in form) is always null. 
nd.SelectNodes("/input") //returns null. 
nd.SelectNodes("./input") //returns null. 
"//form[@id='form1']/input" //returns null. 

입니다 발생의. 크롬 개발자 콘솔에서 동일한 xpath를 시도했는데 정확히 원하는 방식으로 작동합니다. HTMlAgility 팩은 파일이나 웹에서 HTML을 읽는 데 문제가 있습니까?

+0

문제는 무엇인가? –

+0

htmlNodecollection에서 Forms 태그의 모든 자식 노드를 원합니다. –

답변

0

시도 :

HtmlNode.ElementsFlags.Remove("form"); 

HtmlAgilityPack의 기본 동작은 모든 폼의 내부 요소를 자식 대신 형제로 추가합니다. 위의 명령문은 해당 동작 (입력 태그를 의미)이 자식 노드로 표시되도록 동작을 변경합니다.

코드는 다음과 같습니다

HtmlNode.ElementsFlags.Remove("form"); 
HtmlDocument doc = new HtmlDocument();  
doc.LoadHtml(File.ReadAllText(@"C:\sample.html")); 
HtmlNode nd = doc.DocumentNode.SelectSingleNode("//form[@id='form1']"); 
etc... 

참조 :

  1. 버그 문제 & 수정 : http://htmlagilitypack.codeplex.com/workitem/23074
  2. 는 CodePlex 포럼 게시물 : http://htmlagilitypack.codeplex.com/discussions/247206
+1

: D 하 하 감사합니다. 그게 내 htmlagility DLL을 정기적으로 업데이 트했습니다 같아요. –

+0

nuget ftw;) -> 네, 그것은 1.4.5 이후 v 1.4.5, 현재 버전은 1.4.6입니다. 기꺼이 도와주세요. –

1

html이 잘못되어 html 민첩성 팩이 제대로 작동하지 않을 수 있습니다.

시도>에서 입력 요소의 닫는 태그를 문서의 시작 부분에 DOCTYPE (그리고 XML 네임 스페이스)를 추가 및 변경 문서를로드하기 전에 다음 문을 추가 />

+0

샘플 페이지 <양식 액션 = "demo_form.asp"ID = "Form1에"방법 = "GET" > 이름 :
성 :
http://validator.w3.org/check로 확인 : 통과. 하지만 같은 오류가 발생합니다. : –

관련 문제