Grab custom tags with Html Agility Pack

Asked

Viewed 544 times

3

I’m using the plugin Html Agility Pack to manipulate html

And I would like him to capture my elements that use tag customized

I tried it this way:

 HtmlDocument html = new HtmlDocument();
 html.Load(new StringReader(Document.Content)); //Aqui é o meu html, ele não possui <body> ele vem carregado do banco

 var teste = html.DocumentNode.SelectNodes("//Tag-Teste"); //no html está <tag-teste>conteudo</tag-teste>

but the variable teste get back to me null

  • You can post a piece of this HTML?

  • @Qmechanic73, the error was because I was using the uppercase initials in Selectnodes, in html, are in that format, but it did not capture, I switched to minuscules only in Selectnodes, it worked, I do not know the reason, so I will not formulate an answer, if you know, formulate

  • I’ll risk an answer. :)

2 answers

3


Solution found by the author: The problem was using the initials of tag capital letters. Instead of Tag-Teste, utilise tag-teste.

The Html Agility Pack deals with the HTML insensitively ¹ in relation to the case-sensitive, however, the XHTML nay.

Knowing this, when you use a Xpath, one should use tags written in minuscule letters.

The section 4.2 of this page quote this.

XHTML documents must use lowercase for all HTML elements and attribute names. This difference is required because XML is case-sensitive

For example <li> and <LI> sane tags different.

¹ The author of Html Agility Pack mentioned this in that comment in the OS.

2

I was able to extract the contents with the code below:

// no html está <tag-teste>conteudo</tag-teste>
var teste = html.DocumentNode.SelectNodes("//tag-teste"); 

foreach (var conteudo in teste)
{
    MessageBox.Show(conteudo.InnerText);
}
  • and how is your html? Because the code is the same, and my "test" comes null

  • haha, slutty, he’s case-sensitive to fetch

  • Did you get it ? I put my html in a string to simulate the return behavior of the database.

Browser other questions tagged

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