-5

我有一个 html 字符串,我想解析它;

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head id="Head1" runat="server">
        <title></title>
    </head>
    <body>
        <table>
            <tr class="MyColumn">
                <td colspan="2">
                    &nbsp;&nbsp;<b>Team List</b>
                </td>
            </tr>
            <tr valign="top">
                <td width="313">
                    &nbsp;<img src="weight.gif" width="11" height="10" alt="Encumbrance: 0" title="Encumbrance: 0">&nbsp;
                    <a href="Test.asp?action=ViewItemDetails&ItemTypeiD=1">Real Madrid</a>
                </td>
                <td align="right" width="140">
                </td>
            </tr>
            <tr valign="top">
                <td width="313">
                    &nbsp;<img src="weight.gif" width="11" height="10" alt="Encumbrance: 1" title="Encumbrance: 1">&nbsp;
                    <a href="Test.asp?action=ViewItemDetails&ItemTypeID=2">Barcelona</a>
                </td>
                <td align="right" width="140">
                </td>
            </tr>
            <tr class="MyColumn">
                <td colspan="2">
                    &nbsp;&nbsp;<b>Money List</b>
                </td>
            </tr>
            <tr valign="top">
                <td width="313">
                    &nbsp;<img src="weight.gif" width="11" height="10" alt="Encumbrance: 0" title="Encumbrance: 0">&nbsp;
                    <a href="Test.asp?action=ViewItemDetails&ItemTypeiD=1">$</a>
                </td>
                <td align="right" width="140">
                </td>
            </tr>
            <tr valign="top">
                <td width="313">
                    &nbsp;<img src="weight.gif" width="11" height="10" alt="Encumbrance: 1" title="Encumbrance: 1">&nbsp;
                    <a href="Test.asp?action=ViewItemDetails&ItemTypeID=2">€&lt;/a>
                </td>
                <td align="right" width="140">
                </td>
            </tr>            
        </table>
    </body>
    </html>

我怎样才能做到这一点;

此致

4

4 回答 4

1
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(html);

var teams = doc.DocumentNode.SelectNodes("//td[@width='313']")
                .Select(td => new TeamClass
                {
                    TeamName = td.Element("a").InnerText,
                    TeamId = HttpUtility.ParseQueryString(td.Element("a").Attributes["href"].Value)["ItemTypeID"]
                })
                .ToList();
于 2012-11-02T12:07:27.917 回答
1

看看这个 lib http: HTML Agility Pack
它可以帮助您进行 HTML 解析。

于 2012-11-02T11:47:03.453 回答
0

您可以使用正则表达式

String html; //your html string
String pattern = @"action=ViewItemDetails&ItemType[I|i]D=(\d*)"">(.*)</a>";
MatchCollection matches = Regex.Matches(html, pattern);
var list = new List<TeamClass>();
foreach (Match match in matches)
{
    TeamClass team = new TeamClass();
    team.TeamName = match.Groups[2].Value;
    team.TeamId = Int32.Parse(match.Groups[1].Value);
    list.Add(team);
}
于 2012-11-02T11:55:08.030 回答
0

尝试HTML 敏捷性

尝试类似(未经测试的代码):

var TeamList = from lnks in document.DocumentNode.Descendants()
               where lnks.Name == "a" && 
                    lnks.Attributes["href"] != null && 
                    lnks.InnerText.Trim().Length > 0
               select new
               {

                  TeamId= (lnks.Attributes["href"].Value).
                          Substring((lnks.Attributes["href"].Value).Length-1, 1),
                  TeamName= lnks.InnerText
               };
于 2012-11-02T12:01:32.547 回答