Search code examples
c#iframehtml-agility-pack

c# get iframe kod with htmlagilitypack?


I am using C# with HtmlAgilityPack and I want to get iframe kod froma a website's html.

This is the iframe area:

<div class="playercont">
   <div id="singlePlay" class="dp_player" data-o="" data-s="">
      <p><iframe src="video.com/sadas" scrolling="no" frameborder="0" width="640" height="360" allowfullscreen="true" webkitallowfullscreen="true" mozallowfullscreen="true"></iframe></p>
   </div>
</div>

I want to get that iframe with console or application, the output I want is:

<iframe src="video.com/sadas" scrolling="no" frameborder="0" width="640" height="360" allowfullscreen="true" webkitallowfullscreen="true" mozallowfullscreen="true"></iframe>

Please give me some examples or the exact code for this.

****UPDATE : After some minutes, I got the code with a little mistake here is the code:

var url = "my html url";

var httpClient = new HttpClient();
var html =  await httpClient.GetStringAsync(url);
var htmlDocument = new HtmlDocument();
htmlDocument.LoadHtml(html);


HtmlNode divContainer = htmlDocument.DocumentNode.SelectSingleNode("//div[@class='someclass']");


Console.WriteLine(divContainer.InnerHtml);

The above code gave me this:

<p><iframe src="video.com/sadas" scrolling="no" frameborder="0" width="640" height="360" allowfullscreen="true" webkitallowfullscreen="true" mozallowfullscreen="true"></iframe></p>

Finally, I want to exclude "p" tag from output, please help me with that.


Solution

  • You should be able to grab the iframe from the divContainer node and then use OuterHtml to get the html content:

    HtmlNode divContainer = htmlDocument.DocumentNode.SelectSingleNode("//div[@id='singlePlay']");
    
    HtmlNode iframe = divContainer.SelectSingleNode("//iframe");
    
    Console.WriteLine(iframe.OuterHtml);