1
votes

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

3

3 Answers

0
votes

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();
}
0
votes

@Orel Eraki - Thank you. I did it myself 3 min ago though but I will use your solution instead since it has only one foreach loop. Anyway here is my solution

     foreach (HtmlNode nodeItem in doc.DocumentNode.Descendants("div").Where(p => p.GetAttributeValue("class", "def").Equals("main-class")))
         {
             foreach (HtmlNode nodeAItem in nodeItem.Descendants("a"))
             {
                Debug.WriteLine(nodeAItem.GetAttributeValue("href", "def"));
                foreach (HtmlNode nodeIMAGEitem in nodeAItem.Descendants("img"))
                 {
                     Debug.WriteLine(nodeIMAGEitem.GetAttributeValue("src", "def"));
                     Debug.WriteLine(nodeIMAGEitem.GetAttributeValue("alt", "def"));
                 }                    
             }
          }
0
votes

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();