I basically try to use htmlagilitypack to parse a piece of html and use linq to put it into object for uses with other piece of my code. I've got below two code snippets where #1 uses linq and does not work, but #2 uses a for loop and works.
Two pieces of codes are nearly identical except for the usage of linq. In that sense, I guess I have missed something in linq that prevents me from making it work?
Appreciate if anyone can help me take a look
using linq - it does not work. res ends up being null
res = from li in ul.Elements("li").Where(i => GetClass(i) != "titles-h") select new Post() { title = li.Element("h2").InnerText, // .... };
no linq - it works
foreach (var li in ul.Elements("li")) { if (GetClass(li) != "titles-h") ress.Add(new Post() { title = li.Element("h2").InnerText, // .... }); }
implementation of GetClass
static string GetClass(HtmlNode n)
{
if (n.Attributes["class"] != null)
return n.Attributes["class"].Value;
else
return "";
}