none
c# get iframe kod with htmlagilitypack? RRS feed

  • Question

  • 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 ;

    <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 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);

    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>

    • Moved by Fei Hu Thursday, March 22, 2018 1:44 AM Third party libraries related
    Sunday, March 18, 2018 2:57 PM

All replies

  • Hi,

    HtmlAgilityPack use XPath to parse HTML, you need understand XPath syntax first. See:

    https://www.w3schools.com/xml/xpath_syntax.asp

    SelectSingleNode("//div[@class='someclass']"); : mean that get "class" attributes equal to 'someclass'  from the current node that match the selection no matter where they are .

    You can get the first iframe Node as below.

      var outerHtml = htmlDocument.DocumentNode.SelectSingleNode("//iframe").OuterHtml;

    You can get all of iframe Node in htmlDoc as below.

      var collection = htmlDocument.DocumentNode.SelectNodes("//iframe");

    Best Regards,

    Bob


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Edited by Bob Ding Monday, March 19, 2018 2:20 AM
    • Proposed as answer by Bob Ding Thursday, March 22, 2018 1:18 AM
    Monday, March 19, 2018 2:17 AM