Linq to XML VB.NET Select and edit rows and cells from a HTML table -
i have plain table made of rows (tr) , cells (td) no specific attributes.
i can read , parse xelement, need use linq xml , perform calculations , changes on cells based on position in row.
<tr><td>a</td><td>12</td><td>2</td><td>result</td></tr> <tr><td>ba</td><td>3.65</td><td>6</td><td>result</td></tr>
for instance need add values of cells 2 , 3 write result cell 4 each row.
i can find plenty of examples given cell has name attribute, bot none can select rows , each row apply calculation on cells based on relative position.
one possible way, assuming table rows has consistent structure* :
dim xml = <table> <tr><td>a</td><td>12</td><td>2</td><td>result</td></tr> <tr><td>ba</td><td>3.65</td><td>6</td><td>result</td></tr> </table> 'loop through table rows' each row xelement in xml.<tr> 'sum value of 2nd & 3rd cells' dim sum = row.<td>.skip(1).take(2).sum(function(x) cdec(x)) 'get last cell sum result displayed' dim result = row.<td>.last() 'update last cell sum result' result.value = sum.tostring(cultureinfo.invariantculture) next console.writeline(xml.tostring())
console output :
<table> <tr> <td>a</td> <td>12</td> <td>2</td> <td>14</td> </tr> <tr> <td>ba</td> <td>3.65</td> <td>6</td> <td>9.65</td> </tr> </table>
* : every row has 4 cells, 2nd & 3rd column contain valid decimal string
Comments
Post a Comment