Empty Data in HtmlAgilityPack

Yusuf 711 Reputation points
2021-04-16T06:57:54.087+00:00

Hi
I'm trying to get meta data information from website.
I'm using this code
But it gives a null or empty value.
Here is my code:

using HtmlAgilityPack;
using System;
using System.Drawing;
using System.Net;
using System.Windows.Forms;


    namespace WindowsFormsApp2
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            private void button1_Click(object sender, EventArgs e)
            {
                GetMetaDataFromUrl(textBox1.Text);
                MetaInformation info = new MetaInformation(textBox1.Text);
                label1.Text = info.Title;
                label2.Text = info.Description;
                label3.Text = info.SiteName;

                var request = WebRequest.Create(info.ImageUrl);

                using (var response = request.GetResponse())
                using (var stream = response.GetResponseStream())
                {
                    pictureBox1.Image = Bitmap.FromStream(stream);
                }
            }

            public static MetaInformation GetMetaDataFromUrl(string url)
            {
                // Get the URL specified
                var webGet = new HtmlWeb();
                var document = webGet.Load(url);
                var metaTags = document.DocumentNode.SelectNodes("//meta");
                MetaInformation metaInfo = new MetaInformation(url);
                if (metaTags != null)
                {
                    int matchCount = 0;
                    foreach (var tag in metaTags)
                    {
                        var tagName = tag.Attributes["name"];
                        var tagContent = tag.Attributes["content"];
                        var tagProperty = tag.Attributes["property"];
                        if (tagName != null && tagContent != null)
                        {
                            switch (tagName.Value.ToLower())
                            {
                                case "title":
                                    metaInfo.Title = tagContent.Value;
                                    matchCount++;
                                    break;
                                case "description":
                                    metaInfo.Description = tagContent.Value;
                                    matchCount++;
                                    break;
                                case "twitter:title":
                                    metaInfo.Title = string.IsNullOrEmpty(metaInfo.Title) ? tagContent.Value : metaInfo.Title;
                                    matchCount++;
                                    break;
                                case "twitter:description":
                                    metaInfo.Description = string.IsNullOrEmpty(metaInfo.Description) ? tagContent.Value : metaInfo.Description;
                                    matchCount++;
                                    break;
                                case "keywords":
                                    metaInfo.Keywords = tagContent.Value;
                                    matchCount++;
                                    break;
                                case "twitter:image":
                                    metaInfo.ImageUrl = string.IsNullOrEmpty(metaInfo.ImageUrl) ? tagContent.Value : metaInfo.ImageUrl;
                                    matchCount++;
                                    break;
                            }
                        }
                        else if (tagProperty != null && tagContent != null)
                        {
                            switch (tagProperty.Value.ToLower())
                            {
                                case "og:title":
                                    metaInfo.Title = string.IsNullOrEmpty(metaInfo.Title) ? tagContent.Value : metaInfo.Title;
                                    matchCount++;
                                    break;
                                case "og:description":
                                    metaInfo.Description = string.IsNullOrEmpty(metaInfo.Description) ? tagContent.Value : metaInfo.Description;
                                    matchCount++;
                                    break;
                                case "og:image":
                                    metaInfo.ImageUrl = string.IsNullOrEmpty(metaInfo.ImageUrl) ? tagContent.Value : metaInfo.ImageUrl;
                                    matchCount++;
                                    break;
                            }
                        }
                    }
                    metaInfo.HasData = matchCount > 0;
                }
                return metaInfo;
            }
        }
    }

How can I make this code work?

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,856 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,377 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 112.9K Reputation points
    2021-04-16T18:40:44.21+00:00

    Try replacing

    GetMetaDataFromUrl(textBox1.Text);
    MetaInformation info = new MetaInformation(textBox1.Text);
    

    with

    MetaInformation info = GetMetaDataFromUrl(textBox1.Text);
    
    0 comments No comments

0 additional answers

Sort by: Most helpful