Madwin74

GetWebpageMetafiles

May 7th, 2023 (edited)
915
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.38 KB | Source Code | 0 0
  1. /***********************************************************
  2.  **                                                       **
  3.  **   Codesnippets written by Madwin for MMP 2023         **
  4.  **                                                       **
  5.  **   Not to be used freely. the code still has errors    **
  6.  **   It's intended to be used as starting point.         **
  7.  **   Code has not been checked and might still need      **
  8.  **   (a lot of) work.                                    **
  9.  **                                                       **
  10.  **   © MadMedia Productions 2023                         **
  11.  **                                                       **
  12.  ***********************************************************/
  13.  
  14.  
  15.  
  16. /*
  17. C# method that you can use to get all meta elements from a web page
  18. loaded in a WebBrowser control:
  19. */
  20.  
  21.  
  22. using System.Collections.Generic;
  23. using System.Windows.Forms;
  24.  
  25. namespace MetaFileGetter
  26. {
  27.     public static class MetaFileGetter
  28.     {
  29.         public static List<HtmlElement> GetAllMetaFiles(WebBrowser webBrowser)
  30.         {
  31.             List<HtmlElement> metaElements = new List<HtmlElement>();
  32.  
  33.             foreach (HtmlElement metaElement in webBrowser.Document.GetElementsByTagName("meta"))
  34.             {
  35.                 metaElements.Add(metaElement);
  36.             }
  37.  
  38.             return metaElements;
  39.         }
  40.     }
  41. }
  42.  
  43. /*
  44. This method takes a WebBrowser object as a parameter and returns a List
  45. of all meta elements found in the web page. The List is created and
  46. initialized to be empty at the beginning of the method.
  47.  
  48. The method then loops through all meta elements in the web page using the
  49. GetElementsByTagName method of the HtmlDocument class. For each meta element
  50. found, it adds it to the List using the Add method of the List class.
  51.  
  52. Finally, the method returns the List of meta elements.
  53.  
  54. To use this method, you can call it and pass in a WebBrowser object that
  55. you have already navigated to the web page. For example:
  56. */
  57.  
  58.  
  59.  
  60. WebBrowser webBrowser = new WebBrowser();
  61. webBrowser.Navigate("https://example.com");
  62. List<HtmlElement> metaElements = MetaFileGetter.GetAllMetaFiles(webBrowser);
  63.  
  64. /*
  65. Note that this method simply returns a List of HtmlElement objects without
  66. filtering by name or any other attribute. You can modify the code to filter
  67. the List by attribute value or other criteria as needed.
  68. */
Advertisement
Add Comment
Please, Sign In to add comment