Regex to HtmlAgilityPack C#

c# html-agility-pack regex

Question

I want to know how to convert my code that uses regex to match website's strings in other that uses the HtmlAgilityPack library.

Example code:

<div class="element"><div class="title"><a href="127.0.0.1" title="A.1">A.1</a></div></div>
<div class="element"><div class="title"><a href="127.0.0.1" title="A.2">A.2</a></div></div>

My current code is the following:

List<string> Cap = new List<string>();
WebClient web = new WebClient();
string url = web.DownloadString("127.0.0.1");
MatchCollection cap = Regex.Matches(url, "title=\"(.+?)\">", RegexOptions.Singleline);
foreach (Match m in cap)
{
     Cap.Add(m.Groups[1].Value.ToString());
}
lst_Cap.ItemsSource = Cap;

And it works.

I've tried with HtmlAgilityPack:

HtmlDocument Web = web.Load("127.0.0.1"); // 127.0.0.1 for example
List<string> Cap = new List<string>();
foreach (HtmlNode node in Web.DocumentNode.SelectNodes("//*[@id=\"content\"]/div/div[3]/div[2]/div[1]/a"))
{
    Cap.Add(node.InnerHtml);
}

But it adds only A.1.

How can I do?

Accepted Answer

Your regex "title=\"(.+?)\">" matches and captures any title attribute, in any tags inside the HTML document.

So, use another code with //*[@title] XPath that gets any element nodes (*) that contain a title attribute, and then just iterate through the attribute nodes and once its name is title, add the value to the list:

var nodes = Web.DocumentNode.SelectNodes("//*[@title]");
if (nodes != null)
{
   foreach (var node in nodes)
   {
       foreach (var attribute in node.Attributes)
           if (attribute.Name == "title")
               Cap.Add(attribute.Value);
   }
}

Or using LINQ:

var nodes = Web.DocumentNode.SelectNodes("//*[@title]");
var res = nodes.Where(p => p.HasAttributes)
                 .Select(m => m.GetAttributeValue("title", string.Empty))
                 .Where(l => !string.IsNullOrEmpty(l))
                 .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