DocumentNode.SelectSingleNode Null Reference Exception

Feb 2, 2011 at 4:23 PM

I am parsing a large database of emails.  I have already filtered out all the empty strings and text/plain mails before calling the following code.  However I occassioaly get an email that has no body tag.  This could be occuring because I am messing up on the content-type filtering, but is there any way for me to check the node to see if it exists without getting an unhandle exception?

 

HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();           
            doc.LoadHtml(html);      
           
            string text = doc.DocumentNode.SelectSingleNode("//body").InnerText;
Feb 3, 2011 at 8:05 AM

HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();           
doc.LoadHtml(html);           
var node = doc.DocumentNode.SelectSingleNode("//body");
if (node != null)
{
     //do something with node
     string text = node.InnerText;
}