i have the follow code:
searchResults.SearchResultCollection.Add(
new SearchResult()
{
Header =
HttpUtility.HtmlDecode(
htmlDocument.DocumentNode
.SelectSingleNode(initialXPath + "h3")
.InnerText),
Link = HttpUtility.HtmlDecode(
htmlDocument.DocumentNode
.SelectSingleNode(initialXPath + "div/cite")
.InnerText)
}
);
Sometimes htmlDocument.DocumentNode.SelectSingleNode(….) returns null and my application crashed with NullReferenceException. Ofcourse i can write code which check returned value on null reference but then code will be overly verbose. What is graceful way to do that?
You could create an extension method on XmlNode, like so:
Personally, I’d probably just suck it up and put in the null tests explicityly, though 🙂