2012-10-29 4 views
0
을 대체

가능한 중복 : 내가 HTML 파일을 구문 분석하고
What is the best way to parse html in C#?HTML 찾아 HREF 태그

. html의 모든 href 태그를 찾고 을 텍스트 대체 버전으로 바꿔야합니다.

다음은 예입니다.

Original Text: <a href="http://foo.bar">click here</a> 
replacement value: click here <http://foo.bar> 

어떻게 처리합니까?

+3

큐 정규식 불꽃 전쟁. – JDB

+0

은 정규 표현식과 역 참조를 사용합니다. – entonio

+0

@ Cyborgx37 그는'regex'를 요구하지 않습니다. 질의는 ** 유효합니다 ** – Anirudha

답변

4

이 같은 코드의 Html Agility Pack library을 사용할 수

 HtmlDocument doc = new HtmlDocument(); 
     doc.Load(myHtmlFile); // load your file 

     // select recursively all A elements declaring an HREF attribute. 
     foreach (HtmlNode node in doc.DocumentNode.SelectNodes("//a[@href]")) 
     { 
      node.ParentNode.ReplaceChild(doc.CreateTextNode(node.InnerText + " <" + node.GetAttributeValue("href", null) + ">"), node); 
     } 

     doc.Save(Console.Out); // output the new doc. 
+0

Simon이 추천하는 라이브러리 (http://meta.stackexchange.com/questions/156184에 따라 필요)는 저자의 저자입니다. 현재 주목할만한 경쟁자는 [CsQuery] (https://github.com/jamietre/CsQuery)입니다. –