2012-04-18 2 views
0

테이블 형식의 이전 데이터를 새로운 형식으로 변환하는 작업을했습니다. 다음과 같이HTML 문서의 표를 파서 TR과 TD를 추출합니다. HTML Agility 팩 포함

올드 더미 데이터는 다음과 같습니다

<table> 
<tr> 
<td>Some text 1.</td> 
<td>Some text 2.</td> 
</tr> 
..... //any number of TRs goes here 
</table> 

문제는 새로운 데이터가이 형식에 있어야한다는 것입니다 :

일부 텍스트 1 - 텍스트 2. .... 필요한 사항의

요약 여기에 수행 할 :

이 테이블의 모든 거래 정보 저장소를 찾습니다. 각 TR에 대해 첫 번째 TD를 찾고 "-"로 구분 된 두 번째 TD와 연결합니다.

VB.Net에서 HTML 민첩성 팩을 사용하고 있습니다.

도와주세요.

감사합니다.

답변

0

Linq 및 HtmlAgilityPack을 사용하여 테이블 노드에서 모든 td를 가져올 수 있으며이 노드의 모든 InnerText를 가져 와서 새 TR/TD를 만들 수 있습니다.

// tableNode is the <table> HtmlNode. If you know where is this table you can use XPath to find him. 

Dim sb As New StringBuilder() 
For Each childNode As HtmlNode In tableNode.DescendantNodes().Where(Function(n) n.Name = "td") 
    sb.Append(String.Format("{0} - ", childNode.InnerText)) 
Next 

tableNode.RemoveAllChildren() 

Dim newTrNode As HtmlNode = tableNode.OwnerDocument.CreateElement("tr") 
Dim newTdNode As HtmlNode = tableNode.OwnerDocument.CreateElement("td") 

newTdNode.InnerHtml = sb.ToString() 
newTrNode.AppendChild(newTdNode) 

tableNode.AppendChild(newTrNode) 

가 나는

도움이되기를 바랍니다