5
When calling the method, an HTML component enumerable should be returned.
I’m using the HTML Agility Pack to read an HTML file. The same method works as expected when removing the yield
and add manually 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, through the method GetNodesByName
or GetNodesByNameList
i can retrieve a list of any structure node 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 at the beginning, the Getnodesbynamelist method returns all items, in this case type article found in the file structure, but the same doesn’t happen when I use Yield.
Since we don’t know what the expected result is, it’s difficult to help. We’re not even seeing the code that produces the variable you’re referencing. Give us more information so we understand the problem.
– Maniero
I added some more information.
– Italo Pessoa
Much improved but there is still not enough information to identify what is wrong. I do not know for example what is serving the
GetNodesByNameList
to define the problem. But it certainly lacks information to know how you expect to arrive at the expected result, what data is being used to produce this result. On the other hand, perhaps you can better describe what problem you are encountering so that someone can provide you with a solution.– Maniero
I added some more information and the data used
– Italo Pessoa
Only when searching for the "article" does it give the problem? Or with "Section" too? Are there other calls before? How is this
GetNodeById
? It is difficult to see the whole. The problem may be occurring because of previous problems or in parts you are not demonstrating. But I think for example that the method withyield
works but does not return what you But it’s still just a kick, I don’t know if I understand the whole problem.– Maniero
the first two lines are working correctly. Note that the method
GetNodesByNameList
has the same structure as the methodGetNodesByName
, but in the case it uses theyield
, using the list all these Articles are returned and withyield
that doesn’t happen. It’s clearer now?– Italo Pessoa
I think I understand the problem. It seems to me that you don’t understand the workings of
yield
. It doesn’t work like it seems you expect. You know it’s areturn
, that it closes the execution in the first interaction of thefor each
? Of course, the interaction can be picked up where it left off on the next call. I’m not sure what your goal is but it seems to me that in this case you can’t use theyield
.– Maniero
The first call of the method is returned to "Section" and nothing else. Then you will search for "article" and have nothing to find. I would probably find if I called the method by searching for "Section" more often, then I would read the whole node. Without the
yield
comes the whole node, so it works. You have to know that a method that returns ayield
within a loop qq will execute a step from this loop in each call to method. If you want it to come 10link
s, will have to call theGetNodesByName
10 times.– Maniero