Quantcast
Channel: htmlagilitypack Forum Rss Feed
Viewing all articles
Browse latest Browse all 655

New Post: SelectNodes to return empty HtmlNodeCollection if no matching node is found

$
0
0

Then use the LINQ interfaces of Descendants and Elements.

SelectNodes matches the .NET System.Xml API for which it was built upon.

I agree this should be overhauled to return an empty list but the fact is there are thousands of applications out there that could break if this is changed in a minor point release. There were new LINQ like functions added of Descendants, Elements and DescendantNodes to more closely match the LINQ to XML interface.

This is one of the things I wanted to address in 2.0 if I can ever get the time to work on it. The actual current implementation with HtmlNodeCollection needs to be tossed out and replaced with a yield return and an IEnumerable<HtmlNode> return type;

For the people that want this, an extension method can solve it

public static IList<HtmlNode> SelectNodesAsList(this HtmlNode node, string xpath)
{
	var list = node.SelectNodes(xpath);
	if (list == null)
		return new List<HtmlNode>();

	return list;

}

Viewing all articles
Browse latest Browse all 655

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>