How can i parse html string [closed]

白昼怎懂夜的黑 提交于 2019-12-14 04:13:33

问题


I cant edit my code so you can delete this post


回答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();



回答2:


Have a look at this lib http:HTML Agility Pack.
It helps you with HTML parsing.




回答3:


You can use Regular expression

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);
}



回答4:


Try Html Agility:

try something like (Untested Code):

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
               };


来源:https://stackoverflow.com/questions/13194614/how-can-i-parse-html-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!