0

我已经完成了将表格格式的旧数据转换为新格式的工作。

旧的虚拟数据如下:

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

问题是新数据需要采用这种格式:

一些文字 1. - 一些文字 2.......

这里需要做的总结:

查找表中的所有 TR。对于每个 TR 找到第一个 TD 并与用“ - ”分隔的第二个 TD 连接。

我正在使用带有 VB.Net 的 HTML 敏捷包。

请帮忙。

谢谢并恭祝安康。

4

1 回答 1

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)

我希望它有帮助

于 2012-05-03T08:32:47.870 回答