C# - Using HTMLAgilityPack to convert a Node to String

c# dom html-agility-pack selectsinglenode

Question

I am trying to convert a selected span tag's inner text to a string.

<span id="course-text">ELEC1700/ELEC1700 2012 S1: Computer Engineering 1</span>

So the above would become: "ELEC1700/ELEC1700 2012 S1: Computer Engineering 1"

I have tried a couple of combinations using innerText and getAttributes, I just can't get it to work.

string name;

WebClient client = new WebClient();

HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument();

htmlDoc.Load(new System.IO.StringReader(client.DownloadString(urlTxtBx.Text)));

name = htmlDoc.DocumentNode.SelectSingleNode("//span[@id='course-text']").InnerText;

Accepted Answer

I was missing the client.DownloadString on the urlTxtBx.Text.

Above code now works fine :)




Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why