I'm getting a nullreferenceexception
using the htmlagilitypack
when my search returns nothing. I need to know how to handle this in code. I'm trying to use ??
but I'm both not using it right and not really sure how to use it anyway. I really just want to know how to run some method if nodes
is empty. I could probably just check with an IF if there's no better way.
public DataTable tableIntoTable(HtmlDocument doc)
{
var table = new DataTable("MyTable");
table.Columns.Add("raw", typeof(string));
var xpath = @"//th[@class='ddlabel'] | //table[not(.//*[contains(@*,'pldefault') or contains(@*,'ntdefault') or contains(@*,'bgtabon')])]";
var nodes = doc.DocumentNode.SelectNodes(xpath);
foreach (var node in nodes ?? new HtmlAgilityPack.HtmlNodeCollection {null})
//new is underlined in red, not sure how it's supposed to work
{
table.Rows.Add(node.InnerHtml);
}
return table;
}