如何使用正则表达式解析字符串

时间:2012-05-11 14:34:32

标签: c#

如何从此文本中获取List():

For the iPhone do the following:<ul><li>Go to AppStore</li><li>Search by him</li><li>Download</li></ul>

应该包括:转到AppStore,                      通过他搜索,                      下载

2 个答案:

答案 0 :(得分:5)

将字符串加载到HTML Agility Pack,然后选择所有li元素内部文本。

HtmlDocument doc = new HtmlDocument();
doc.LoadHtml("following:<ul><li>Go to AppStore</li><li>Search by him</li><li>Download</li></ul>");

var uls = doc.DocumentNode.Descendants("li").Select(d => d.InnerText);
foreach (var ul in uls)
{
    Console.WriteLine(ul);
}

答案 1 :(得分:2)

包装XML根元素并使用LINQ to XML:

var xml = "For the iPhone do the following:<ul><li>Go to AppStore</li><li>Search by him</li><li>Download</li></ul>";
xml = "<r>"+xml+"</r>"; 
var ele = XElement.Parse(xml);  
var lists = ele.Descendants("li").Select(e => e.Value).ToList();

lists中返回:

Go to AppStore 
Search by him 
Download