I am using HtmlAgilityPack to parse some data and writing statements like the below one to remove unwanted contents -
doc.DocumentNode.SelectNodes("//ul").ToList().ForEach(a => a.Remove());
This works well when there's <ul>
element present in the HTML document. However, if there's no <ul>
element, I get a Value cannot by null exception when converting it into .ToList().
I do not want to use the following -
doc.DocumentNode.SelectNodes("//ul")== null ? null : doc.DocumentNode.SelectNodes("//ul").ToList().ForEach(a => a.Remove());
What elegant alternatives do I have?