HtmlAgilityPack parse attributes

c# html-agility-pack

Question

I am trying to parse HTML and I don't know how to use conditions (ex. class name must be X). I know there is a lot of topics about agility pack but I could not find any of them helpful.

<div class="main-class">
<a href="LINK">
<img src="IMAGELINK" alt="SOMETEXT" class="image-class">
</a>
</div>

<p> bla bla </p>

<div class="main-class">
<a href="LINK">
<img src="IMAGELINK" alt="SOMETEXT" class="image-class">
</a>
</div>

<div class="main-class">
<a href="LINK">
<img src="IMAGELINK" alt="SOMETEXT" class="image-class">
</a>
<p> asd sadh awww </p>
</div>

I want to get href, src and alt for each of div with class name "main-class", This is my code but it only print "p" as that is only thing I know how to do.

      HtmlDocument doc = new HtmlDocument();
        doc.LoadHtml(dataString);
         foreach (HtmlNode nodeItem in doc.DocumentNode.Descendants("p").ToArray())
          {
              Debug.WriteLine(nodeItem.InnerText);
          }

I am working on WP app where "SelectNodes" is not supported

Accepted Answer

By using traditional Non-XPath way.

Note: Checking for null-able values omitted.

string dataString = "<div class=\"main-class\"><a href=\"LINK\"><img src=\"IMAGELINK\" alt=\"SOMETEXT\" class=\"image-class\"></a></div><p> bla bla </p><div class=\"main-class\"><a href=\"LINK\"><img src=\"IMAGELINK\" alt=\"SOMETEXT\" class=\"image-class\"></a></div><div class=\"main-class\"><a href=\"LINK\"><img src=\"IMAGELINK\" alt=\"SOMETEXT\" class=\"image-class\"></a><p> asd sadh awww </p></div>";

var doc = new HtmlDocument();
doc.LoadHtml(dataString);

var elements = doc.DocumentNode.Descendants("div").Where(o => o.GetAttributeValue("class", "") == "main-class");
foreach (var nodeItem in elements)
{
    var aTag = nodeItem.Descendants("a").First();
    var aTagHrefValue = aTag.Attributes["href"];

    var imgTag = nodeItem.Descendants("img").First();
    var imgTagSrcValue = imgTag.Attributes["src"];
    var imgTagAltValue = imgTag.Attributes["alt"];

    Console.WriteLine("a href value: {0}", aTagHrefValue.Value);
    Console.WriteLine("img src value: {0}", imgTagSrcValue.Value);
    Console.WriteLine("img alt value: {0}", imgTagAltValue.Value);
    Console.WriteLine();
}

Popular Answer

You can use LINQ for that

var attrs = doc.DocumentNode
               .Descendants("div")
               .Where(d => d.Attributes != null &&
                           d.Attributes.Contains("class") &&
                           d.Attributes["class"].Value.Contains("main-class"))
               .Select(d => new
               {
                   anchor = d.SelectSingleNode("a"),
                   img = d.SelectSingleNode("a") != null 
                                                 ? d.SelectSingleNode("a").SelectSingleNode("img") 
                                                 : null 
               })
               .Select(d => new
               {
                   href = d.anchor != null 
                                   ? d.anchor.GetAttributeValue("href", string.Empty) 
                                   : string.Empty,
                   imgsrc = d.img != null 
                                  ? d.img.GetAttributeValue("src", string.Empty) 
                                  : string.Empty,
                   imgalt = d.img != null 
                                  ? d.img.GetAttributeValue("alt", string.Empty) 
                                  : string.Empty
               })
               .ToList();



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why