How to reference HTML elements without ID in C # Windows Forms

1

Good evening!

I have the following code:

<H3>Dados Cadastrais</H3>
  <table class='dados'>
  <tr>
    <th>Avalista:</th>
    <td>VINICIUS ALVES GONZALEZ</td>
    <th>Contrato:</th>
    <td>72001018  </td>
  </tr>
  <tr>

How can I reference this element using C # to get the client name inside the <td> and the contract number tbm ...

I only get items that have ID using document.getElementById

    
asked by anonymous 01.09.2016 / 01:59

2 answers

2

Have you tried using AngleSharp

  

Install-Package AngleSharp

var xml = @"
<H3>Dados Cadastrais</H3>
<table class='dados'>
<tr>
    <th>Avalista:</th>
    <td>VINICIUS ALVES GONZALEZ</td>
    <th>Contrato:</th>
    <td>72001018  </td>
</tr>";


var parser = new HtmlParser();
var document = parser.Parse(xml);

Console.WriteLine(document.QuerySelector("td:nth-child(2)").InnerHtml);
Console.WriteLine(document.QuerySelector("td:nth-child(4)").InnerHtml);

result:

VINICIUS ALVES GONZALEZ
72001018  
    
02.09.2016 / 03:33
0

You want to say get DOM elements using JavaScript to then send that data to C# ?

You can use in addition to document.getElementById() , document.getElementsByTagName("td")[0] note that index [0] is why in your case it would return the two td 's .

You also have document.getElementsByClassName("nome") note that for this to work, you would have to change the td field where% < html is in your <td class="nome">VINICIUS ALVES GONZALEZ</td> .

You can still get elements for values and any other attribute you want, but you will need a more complex function for that. This function will first grab all the elements and check at all if the attribute you want exists and if the value of this attribute is the same as you want.

    
01.09.2016 / 02:07