Null Reference in HTML Agility Pack

c# html-agility-pack

Question

I'm having some issues with the HTML Agility Pack.

When I apply this function to HTML that doesn't include the desired node, I get a null reference error. It first worked, but suddenly it stopped. This is only a small sample; there are around 10 more foreach loops that choose various nodes.

Why am I misusing this?

public string Export(string html)
{
    var doc = new HtmlDocument();
    doc.LoadHtml(html);
    // exception gets thrown on below line
    foreach (var repeater in doc.DocumentNode.SelectNodes("//table[@class='mceRepeater']"))
    {
        if (repeater != null)
        {
            repeater.Name = "editor:repeater";
            repeater.Attributes.RemoveAll();
        }
    }

    var sw = new StringWriter();
    doc.Save(sw);
    sw.Flush();

    return sw.ToString();
}
1
12
2/22/2012 8:07:28 AM

Accepted Answer

AFAIK, DocumentNode.SelectNodes may come backnull if there are no nodes.

See the following discussion thread on codeplex for the default behavior: Why DocumentNode.SelectNodes produces an empty list

In order to get around this, you may rewrite theforeach block:

var repeaters = doc.DocumentNode.SelectNodes("//table[@class='mceRepeater']");
if (repeaters != null)
{
    foreach (var repeater in repeaters)
    {
        if (repeater != null)
        {
            repeater.Name = "editor:repeater";
            repeater.Attributes.RemoveAll();
        }
    }
}
30
4/27/2011 10:23:02 AM

Popular Answer

You may now set this to prevent SelectNodes from returning null after it has been modified.doc.OptionEmptyCollection = true described in this problem on github.

If there are no nodes that match the query, this will cause it to return an empty collection rather than null (I'm not sure why this wasn't the default behavior, however).



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow