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

本文关键字:单词 获取 HTML 文件 | 更新日期: 2023-09-27 18:18:22

如何使用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>

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

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();

使用c#搜索文本文件并显示行号和包含搜索关键字

的完整行