C# 解析表格单元格C或VisualBasic中的数据/数字
我有一个包含网页html代码的字符串。代码中有一个表我很感兴趣。我想解析表单元格中的数字,并将它们放在文本框中,每个数字都放在自己的文本框中。这是桌子:C# 解析表格单元格C或VisualBasic中的数据/数字,c#,asp.net,vb.net,parsing,html-table,C#,Asp.net,Vb.net,Parsing,Html Table,我有一个包含网页html代码的字符串。代码中有一个表我很感兴趣。我想解析表单元格中的数字,并将它们放在文本框中,每个数字都放在自己的文本框中。这是桌子: <table class="tblSkills"> <tr> <th class="th_first">Strength</th><td class="align_center">15</td> <th>Passing&l
<table class="tblSkills">
<tr>
<th class="th_first">Strength</th><td class="align_center">15</td>
<th>Passing</th><td class="align_center">17</td>
</tr>
<tr>
<th class="th_first">Stamina</th><td class="align_center">16</td>
<th>Crossing</th><td class="align_center"><img src='/pics/star.png' alt='20' title='20' /></td>
</tr>
<tr>
<th class="th_first">Pace</th><td class="align_center"><img src='/pics/star_silver.png' alt='19' title='19' /></td>
<th>Technique</th><td class="align_center">16</td>
</tr>
<tr>
<th class="th_first">Marking</th><td class="align_center">15</td>
<th>Heading</th><td class="align_center">10</td>
</tr>
<tr>
<th class="th_first">Tackling</th><td class="align_center"><span class='subtle'>5</span></td>
<th>Finishing</th><td class="align_center">15</td>
</tr>
<tr>
<th class="th_first">Workrate</th><td class="align_center">16</td>
<th>Longshots</th><td class="align_center">8</td>
</tr>
<tr>
<th class="th_first">Positioning</th><td class="align_center">18</td>
<th>Set Pieces</th><td class="align_center"><span class='subtle'>2</span></td>
</tr>
</table>
如你所见,共有14个数字。更糟糕的是,像19和20这样的数字会被图像替换,而低于6的数字会有一个span类。
我知道我可以使用HTML agility pack或类似的东西,但我自己还不太清楚如何做到这一点,所以我需要您的帮助。您的HTML示例恰好也是很好的XML。您可以使用.net的任何XML读取/解析技术。在C中使用LINQ to XML:
var doc = XDocument.Parse(yourHtml);
var properties = new List<string>(
from th in doc.Descendants("th")
select th.Value);
var values = new List<int>(
from td in doc.Descendants("td")
let img = td.Element("img")
let textValue = img == null ? td.Value : img.Attribute("alt").Value
select int.Parse(textValue));
var dict = new Dictionary<string, int>();
for (var i = 0; i < properties.Count; i++)
{
dict[properties[i]] = values[i];
}
是的,Html敏捷包将是一个不错的选择。你试过用它吗?到目前为止你有什么代码?所以你更喜欢正则表达式来解决这个问题?我试过正则表达式。我没有足够的分数来发布任何代码@Josh:并不是我喜欢这个或那个,我希望它能工作,只是尝试了一些东西,我认为它不能用Regex.string text=。。。;字符串模式=@\d+;在Regex.Matchestext,pattern{string x=m.ToString;}中,foreach Match m只需要这个图像中的一个19,而不是两个。我怎样才能过滤掉它???