如何从HTML文件中获取特定单词的行号

时间:2015-10-19 10:03:13

标签: c# html

如何使用visual c#..从HTML文件中获取Subtotal字符串的行号,表示HTML文件的HTML代码。

我的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>

1 个答案:

答案 0 :(得分: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