0

How can I get List() from this text:

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

that should consist :Go to AppStore, Search by him, Download

0

2 Answers 2

5

Load the string up into the HTML Agility Pack then select all li elements inner text.

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);
}
Sign up to request clarification or add additional context in comments.

1 Comment

+1 for HTML in the wild, you can't rely on well-formed XHTML, so Agility Pack is needed.
2

Wrap in an XML root element and use 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();

Returns in lists:

Go to AppStore 
Search by him 
Download

1 Comment

+1 for using XML, though in small cases as this, is it not faster to just parse the string?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.