Search code examples
c#html-agility-packdomselectsinglenode

C# - Using HTMLAgilityPack to convert a Node to String


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;

Solution

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

    Above code now works fine :)