0

I'm working on a project when I need to copy the innerHTML of a div and send it to a txt file in C#. While I can get the innerHTML of the entire page through this code:

private void button3_Click(object sender, EventArgs e)
    {
        using (WebClient client = new WebClient())
        {
            string htmlCode = client.DownloadString("youtube.com");
            textBox1.Text = htmlCode;
        }

    }

What do I need to do to get the innerHTML of a specific div? Thanks!

Ibra
  • 3
  • 1
  • 3

1 Answers1

1

I have a solution for this using HtmlAgilityPack...

  1. Install HtmlAgilityPack from NuGet.
  2. Use this code.

    HtmlWeb web = new HtmlWeb();
    HtmlDocument dc = web.Load("Your_Url");
    var s = dc.DocumentNode.SelectSingleNode("//div[@id='div_id']").InnerHtml;
    

It will return inner html of div.

Manoj
  • 4,951
  • 2
  • 30
  • 56
  • Thank you! but I get an error that the"HtmlDocument" is an ambiguous reference between (System.windows.forms.HtmlDocument) and (HtmlAgilityPack.HtmlDocument). Would you please tell me how to handle this error? – Ibra Aug 27 '14 at 05:13
  • remove this System.windows.forms.HtmlDocument @Ibra – Manoj Aug 27 '14 at 05:26