0

How to get line number of the Subtotal string from HTML file using visual c#.. in below represented the HTML code of the HTML file.

MY HTML

<tr>
    <td>
        <table width="100%"
               class="sales">
            <!-- Headers -->
            <tr>
                <th align="center">Qty</th>
                <th align="center">Item</th>
                <th align="right">Price</th>
                <th align="right">Amount</th>
            </tr>
            <!-- Rows -->
            <tr class="saleline">
                <td align="left">144</td>
                <td align="left">0002</td>
                <td align="right">5.00</td>
                <td align="right">720.00</td>
            </tr>
            <tr class="saleline">
                <td align="left">8</td>
                <td align="left">0788</td>
                <td align="right">1,200.00</td>
                <td align="right">9,600.00</td>
            </tr>
            <tr class="saleline">
                <td align="left">12</td>
                <td align="left">0013</td>
                <td align="right">15.00</td>
                <td align="right">180.00</td>
            </tr>
            <tr class="saleline">
                <td align="left">144</td>
                <td align="left">120p CR SR 115/=</td>
                <td align="right">115.00</td>
                <td align="right">16,560.00</td>
            </tr>
            <!-- Totals -->
            <tr>
                <td align="right"
                    colspan="3">Subtotal</td>
                <td align="right">27,060.00</td>
            </tr>
            <tr>
                <td align="right"
                    colspan="3">
                    <b>TOTAL</b>
                </td>
                <td align="right">
                    <b>27,060.00</b>
                </td>
            </tr>
            <tr>
                <td align="right"
                    colspan="3">Less Payment</td>
                <td align="right">20,000.00</td>
            </tr>
            <tr class="total">
                <td align="right"
                    colspan="3">
                    <strong>Balance Due</strong>
                </td>
                <td align="right">7,060.00</td>
            </tr>
        </table>
    </td>
</tr>
Shweta Pathak
  • 775
  • 1
  • 5
  • 21
Dilanka Laksiri
  • 408
  • 3
  • 12
  • 1
    Why do you need the line-number? It shouldn't matter. If you want to parse HTML use HtmlAgilityPack. – Tim Schmelter Oct 19 '15 at 10:05
  • @TimSchmelter I would say you're unfair. It's not our place to judge use-cases. – Rasmus Oct 19 '15 at 10:09
  • @Rasmus: i asked why he needs that since it looks like an xy-problem. I didn't judge him. – Tim Schmelter Oct 19 '15 at 10:11
  • @TimSchmelter "It shouldn't matter." - My point is, it matters since that's what he wants to do. – Rasmus Oct 19 '15 at 10:13
  • I tried various method to do that. but it's dosen't work for me.. also i used HtmlAgilitiyPack.. please can you provide some coding example for me? – Dilanka Laksiri Oct 19 '15 at 10:16
  • @MecDuino: what are you trying to do? I can't help with providing a code sample if you don't tell what you're actually trying to achieve. – Tim Schmelter Oct 19 '15 at 10:18
  • @Rasmus: sometimes helping people to find a different apporach helps much more than providing a direct solution to the original question. That's called an [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). I'm fairly sure that this is one. – Tim Schmelter Oct 19 '15 at 10:20
  • @TimSchmelter I might agree with you. We don't know OPs use-case. But there absolutely are valid use-cases for wanting to know which line-number something is present on and the question is simple enough that, provided enough people are interested, the answers will provide several alternatives on how to achieve that. – Rasmus Oct 19 '15 at 10:32

1 Answers1

2
int counter = 0;
string line;

// Read the file and display it line by line.
System.IO.StreamReader file = new System.IO.StreamReader("c:\\test.html");
while((line = file.ReadLine()) != null)
{
    if ( line.Contains("Subtotal") )
    {
        Console.WriteLine (counter.ToString() + ": " + line);
    }

   counter++;
}

file.Close();

search text file using c# and display the line number and the complete line that contains the search keyword

Community
  • 1
  • 1
Toddams
  • 1,489
  • 15
  • 23