HTML Agility Pack Null Reference

孤街醉人 提交于 2019-11-28 12:05:10

AFAIK, DocumentNode.SelectNodes could return null if no nodes found.

This is default behaviour, see a discussion thread on codeplex: Why DocumentNode.SelectNodes returns null

So the workaround could be in rewriting the foreach 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();
        }
    }
}

This has been updated, and you can now prevent SelectNodes from returning null by setting doc.OptionEmptyCollection = true, as detailed in this github issue.

This will make it return an empty collection instead of null if there are no nodes which match the query (I'm not sure why this wasn't the default behaviour to begin with, though)

quillbreaker

As per Alex's answer, but I solved it like this:

public static class HtmlAgilityPackExtensions
{
    public static HtmlAgilityPack.HtmlNodeCollection SafeSelectNodes(this HtmlAgilityPack.HtmlNode node, string selector)
    {
        return (node.SelectNodes(selector) ?? new HtmlAgilityPack.HtmlNodeCollection(node));
    }
}
Shakeel Ahmad

You add simple ? before every . example are given blow:

var titleTag = htdoc?.DocumentNode?.Descendants("title")?.FirstOrDefault()?.InnerText;
s_tranquil

I've created universal extension which would work with any IEnumerable<T>

public static List<TSource> ToListOrEmpty<TSource>(this IEnumerable<TSource> source)
{
    return source == null ? new List<TSource>() : source.ToList();
}

And usage is:

var opnodes = bodyNode.Descendants("o:p").ToListOrEmpty();
opnodes.ForEach(x => x.Remove());
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!