Html Agility Pack C#

Asked

Viewed 164 times

-1

I’d like to know how the // in the Html Agility Pack. Well, the doubt is the following, code:

var t = htmlDoc.DocumentNode.SelectNodes("//table[@class='" + attr + "']").First().SelectNodes("tbody").First()
                    .SelectNodes("tr");

int index = 0;

Empresa empresa = new Empresa();

foreach (var elem in t)
{
    var tdList = elem.SelectNodes("td");
    foreach (var td in tdList)
    {
        if (td.HasClass("higher"))
        {
            index++;
            empresa = new Empresa();
            empresa.nome = td.InnerText.Trim();
        }
        else if (td.HasClass("strong"))
        {
            empresa.nro = index;
            Siglas sigla = new Siglas();
            sigla.Sigla = td.InnerText.Trim();
            sigla.link = td.SelectNodes("a").First().GetAttributeValue("href", "");
            empresa.siglas.Add(sigla);
        }

        if (td == tdList.Last())
            listEmpresa.Add(empresa);
    }
}

On the part that I’m getting the table, if I put

(Without the //)

htmlDoc.DocumentNode.SelectNodes("table[@class='" + attr + "']").First()

It does not bring any object to my list, until then ok, I understood that there // are necessary.

Already in the part I try to catch the href of the tag a

(With //)

sigla.link = td.SelectNodes("//a").First().GetAttributeValue("href", "");

example of caught link = "www.google.com"

(Without //)

sigla.link = td.SelectNodes("a").First().GetAttributeValue("href", "");

example of caught link = "www.google.com/xxxx-xxxx"

Well, my question is as follows. Why I can get the full address of href only if I use without the // ? How does the // in that library? I’ve seen it too .// I’m not sure how it works either. I was able to do what I wanted to get the full address of the "a" tag, but I would like to understand what I did.

1 answer

-1

  • Improve this answer because it is very crude. Explain to the Author of the Answer what he is using and show the evidence. He doesn’t know he’s using Xpath. Show the meaning of what he’s doing as well as the meaning of what he tried to do. Remember the author’s question :"Why can I get the full address of href only if I use it without the // ? How does the // work in this library? I’ve also seen . // I’m not sure how it works either. I was able to do what I wanted to get the full address of the "a" tag, but I would like to understand what I did."

Browser other questions tagged

You are not signed in. Login or sign up in order to post.