c# - How to get all the contents of specified class from string? -
i trying make app windows phone forum. here function far (just taking xaml):
private async void go_click(object sender, routedeventargs e) { httpclient wc = new httpclient(); httpresponsemessage response = await wc.getasync("http://www.myforum.com/"); response.ensuresuccessstatuscode(); string xaml = await response.content.readasstringasync(); textxaml.text = json; }
there no errors in code , xaml.
what want category names of forum. category names have class of "category-name".
how can category names? can them string? have parse string or something?
i think article on codeproject you:
http://www.codeproject.com/tips/804660/how-to-parse-html-using-csharp
it uses library called htmlagilitypack can install via nuget:
http://www.nuget.org/packages/htmlagilitypack
the below example may work purposes due firewall issues haven't been able test works , helps answer question.
using system; using system.collections.generic; using system.linq; using system.net; using system.net.http; using system.text; using htmlagilitypack; namespace htmlparserdemo { class program { // update url page trying parse private const string url = "http://www.bing.com/"; private const string tagname = "a"; private const string classname = "forumtitle"; static void main(string[] args) { try { console.writeline("getting html from: {0}", url); foreach (var category in getcategories(url, tagname, classname)) { console.writeline(category); } } catch (exception exception) { while (exception != null) { console.writeline(exception); exception = exception.innerexception; } } { console.writeline("press key exit..."); console.readkey(true); } } public static ienumerable<string> getcategories(string url, string htmltag, string classname = "") { var response = new httpclient().getbytearrayasync(url).result; string source = encoding.getencoding("utf-8").getstring(response, 0, response.length - 1); source = webutility.htmldecode(source); var result = new htmldocument(); result.loadhtml(source); return result.documentnode.descendants() .where(node => node.name == htmltag && (string.isnullorempty(classname) || (node.attributes["class"] != null && node.attributes["class"].value == classname))) .select(node => node.innertext); } } }
Comments
Post a Comment