Reputation: 9857
I am attempting to scrape a random site for input tags.
So I need to write this in such a way that it will work with most sites.
Currently I have
void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
HtmlAgilityPack.HtmlDocument dom = new HtmlAgilityPack.HtmlDocument();
dom.LoadHtml(e.Result);
var node = dom.DocumentNode.Element("html");
var inputs = node.ChildNodes["body"].Descendants("input");
but this isn't working.
node returns with the data I want but inputs always turns out null.
When I do node.ChildNodes["body"].Descendants().ToList();
I see entries named "input
".
Am I missing something?
Upvotes: 0
Views: 244
Reputation: 89305
Your code is fine, it's just the way you verified was wrong. All those null
values in the screenshot doesn't indicate that the IEnumerable<T>
is empty, you can try to call inputs.ToArray()
or inputs.ToList()
in watch window instead to verify the result.
Remember that IEnumerable<T>
lazy-load the items until necessary, for example, until you iterate through, call ToList()
/ToArray()
, etc.
Upvotes: 1