Create a test version SelectSingleNodeas an extension method.
public static class XmlNodeExtensions
{
public static bool TrySelectSingleNode(this XmlNode node,
string xpath,
out XmlNode result)
{
result = node.SelectSingleNode(xpath);
return (result != null);
}
}
Then you change your code to:
XmlNode node;
searchResults.SearchResultCollection.Add(
new SearchResult
{
Header = HttpUtility.HtmlDecode(
htmlDocument.DocumentNode
.TrySelectSingleNode(initialXPath + "h3", out node)
? node.InnerText
: null),
Link = HttpUtility.HtmlDecode(
htmlDocument.DocumentNode
.TrySelectSingleNode(initialXPath + "div/cite", out node)
? node.InnerText
: null)
});
source
share