When executing the method call, an Enumerable of HTML components should be returned.
I'm using the HTML Agility Pack to read an HTML file. The same method works as expected by removing yield
and manually adding to a list
HtmlNode slideCineAll = GetNodeById(cinema, "slide-cine-all");
HtmlNode section = GetNodeByName(slideCineAll, "section");
IEnumerable<HtmlNode> articles = GetNodesByName(section, "article");
private static IEnumerable<HtmlNode> GetNodesByName(HtmlNode root, string node)
{
foreach (HtmlNode link in root.ChildNodes)
{
if (link.Name.Equals(node))
{
yield return link;
}
}
}
private static List<HtmlNode> GetNodesByNameList(HtmlNode root, string node)
{
List<HtmlNode> nodes = new List<HtmlNode>();
foreach (HtmlNode link in root.ChildNodes)
{
if (link.Name.Equals(node))
{
nodes.Add(link);
}
}
return nodes;
}
This is the result stored in the variable when executing the method
{ConsoleApplication1.Program.GetNodesByName}
node: null
root: null
System.Collections.Generic.IEnumerator<HtmlAgilityPack.HtmlNode>.Current: null
System.Collections.IEnumerator.Current: null
Expected result
values
Count = 20
[0]: Name: "article"}
.
.
.
values[0]
_attributes: {HtmlAgilityPack.HtmlAttributeCollection}
_childnodes: {HtmlAgilityPack.HtmlNodeCollection}
_endnode: Name: "article"}
.
.
.
This is the structure I'm going through, using the method GetNodesByName
or GetNodesByNameList
I can retrieve a list from any node of the html
<div id="slide-cine-all">
<section>
<article>
<!--mais elementos-->
</article>
<article>
<!--mais elementos-->
</article>
<article>
<!--mais elementos-->
</article>
<article>
<!--mais elementos-->
</article>
<article>
<!--mais elementos-->
</article>
<article>
<!--mais elementos-->
</article>
</section>
</div>
As described in the beginning, the GetNodesByNameList method returns all items, in this case of the type article found in the file structure, but not the same when using yield.