I am using asp.net Webmatrix-Razor. I am also using HTMLAgilityPack. I am parsing meta tags to grab the Facebook meta tags from website to post to my site. the code works on all websites except when I try to grab a news article from the Huffington Post. I get “System.NullReferenceException: Object reference not set to an instance of an object.” The error happens at the line with select new. The site does have Facebook meta Tags. there is this meta tag which I heard might cause an issue not sure though.
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" >
Here is the code;
var webGet = new HtmlWeb();
var document = webGet.Load(Request.Form["news_URL"]);
var metaTags = document.DocumentNode.SelectNodes(".//meta");
if (metaTags != null){
var metaOnPage = from lnks in document.DocumentNode.Descendants()
where lnks.Name == "meta" &&
lnks.Attributes["property"] != null
//lnks.InnerText.Trim().Length > 0
select new
{
metaName = lnks.Attributes["property"].Value,
metaContent = lnks.Attributes["content"].Value
};
var Title = "";
var URL = "";
var imgSRC = "";
var Description = "";
foreach(var i in metaOnPage){
if(i.metaName == "og:title"){
Title = i.metaContent;
}else if(i.metaName == "og:url"){
URL = i.metaContent;
}else if(i.metaName == "og:image"){
imgSRC = i.metaContent;
}else if(i.metaName == "og:description"){
Description = i.metaContent;
}
}
Seems likely that this expression is the problem:
lnks.Attributes["content"].Value. You’ve checked that “property” exists, but not “content”. Maybe you want to add the check, since your code seems to assume that content contains something: