如何将HTML文件中的必要信息保存到字符串变量中

本文关键字:信息 保存 变量 字符串 HTML 文件 | 更新日期: 2023-09-27 18:18:15

一个HTML文件是由Android MobileBiz Pro发票应用程序生成的。我试图使软件通过收据打印机打印基于HTML的发票

我需要保存必要的HTML文件的信息到字符串变量如下所述。我尝试使用IndexOf方法。但这对我不起作用。如何使用visual c#获得这些信息?

string subtotal = 2,976.00;
string total = 2,976.00;
string payment= 2,760.00;
string balance= 216.00;

这是一个HTML代码的例子:

<tr><td align="right" colspan="3">Subtotal</td><td align="right">2,976.00</td></tr><tr><td align="right" colspan="3"><b>TOTAL</b></td><td align="right"><b>2,976.00</b></td></tr><tr><td align="right" colspan="3">Less Payment</td><td align="right">2,760.00</td></tr><tr class="total"><td align="right" colspan="3"><strong>Balance Due</strong></td><td align="right">216.00</td></tr>

这是HTML文件的完整HTML代码

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<style>
body {
    font-family:Verdana, Geneva, sans-serif;
    font-size: 8pt;
    padding: 0 50pt 0 50pt;
}
table td, table th, table.sales th, table td.footer-text {
    font-size: 8pt;
}
h1 {
    font-family:Verdana, Geneva, sans-serif;
    padding-bottom:2px;
    margin-bottom:2px;
    color:chocolate;
    text-transform:uppercase;
    font-size: inherit;
    font-size: 1.5em;
}
h2 {
    font-family:Verdana, Geneva, sans-serif;
    padding-bottom:0px;
    margin-bottom:0px;
    color:chocolate;
    text-transform:uppercase;
    font-size: 1.3em;
}
h3 {
    font-family:Verdana, Geneva, sans-serif;
    padding-bottom:2px;
    margin-bottom:2px;
}
table.sales td {
    padding: 4px 10px 4px 10px;
}
table.sales th {
    padding: 5px 10px 5px 10px;
    background-color:#CCC;
}
tr.saleline td {
    border-bottom-color:chocolate;
    border-bottom-width: 1pt;
    border-bottom-style: solid;
    vertical-align: top;
}
.signature {
    display: none;
}
.horizontal-line {
    border: 0;
    height: 4pt;
    color:chocolate;
    background-color: chocolate;
}
.total {
    font-weight:bold;
    font-size:1.1em;
    background-color:#CCC;
}
.block1 {
    text-align:left;
    vertical-align:bottom
}
.block2 {
    text-align:right;
    vertical-align:bottom
}
.block3 {
    text-align:left;
    vertical-align:top;
}
.block4 {
    text-align:left;
    vertical-align:top;
}
.block5 {
    text-align:right;
    vertical-align:bottom;
}
.block6 {
    text-align:left;
    vertical-align:top;
    margin-top: 15px;
}
.block7 {
    text-align:left;
    vertical-align:top;
    margin-top: 15px;
}
.block8 {
    text-align:left;
    vertical-align:bottom;
}
.block9 {
    text-align:center;
    vertical-align:bottom;
}
.block10 {
    text-align:right;
    vertical-align:bottom;
}
.block11 {
    text-align:left;
    padding: 25px 0 15px 0;
}
.extracols {
    border-style:solid; 
    border-color:gray;
}
table.extracols {
    border-top-width: 1pt;
    border-right-width: 0;
    border-bottom-width: 1pt;
    border-left-width: 1pt;
    border-collapse:collapse;
    margin: 0 0 15pt 0;
}
table.extracols th {
    padding: 5px 10px 5px 10px;
    border-top-width: 0;
    border-right-width: 1pt;
    border-bottom-width: 0;
    border-left-width: 0;
    border-color:gray;
    border-style:solid; 
    background-color:#CCC;
}
table.extracols td {
    padding: 4px 10px 4px 10px;
    border-top-width: 0;
    border-right-width: 1pt;
    border-bottom-width: 0;
    border-left-width: 0;
    border-color:gray;
    border-style:solid;
    background-color:#FFF;
}
#footer {
    margin-top: 35px;
}
.footer-text {
    font-size: inherit;
    font-size: 0.97em   
}
</style>
</head>
<body  style="padding: 20 20 20 20">
<table width="100%">
  <tr>
    <td style="padding-bottom:20px"><table width="100%">
        <tr>
          <td style="text-align:left;"></td>
          <td class="block2" align="right"><h3>Y.P.Brothers</h3>
           No:55/B,<br/>Samagipura,<br/>Sewanagala.
<br/>077-6977139
<br/>mecduino@gmail.com
<br/>
           </td>
        </tr>
      </table></td>
  </tr>
  <tr>
    <td><hr class="horizontal-line"/></td>
  </tr>
  <tr>
    <td><table width="100%"> 
        <tr>
          <td style="padding:10px 0 20px 0;"><table width="100%">
              <tr>
                <td width="33%" class="block3"><strong>Bill To</strong><br />
                ANUSHA SURIYA<br/>
                </td>
                <td class="block4"><strong></strong><br />
                </td>
                <td class="block5" align="right"><h1>invoice #1</h1>
                  <b>Date</b>: Oct 9, 2015
<br/><b>Due Date</b>: Oct 9, 2015
                 </td>
              </tr>
            </table></td>
        </tr>
        <tr>
            <td>
            </td>
          </tr>
        <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">12</td>   <td align="left">helaligth 35/=</td>   <td align="right">35.00</td>   <td align="right">420.00</td> </tr> 
  <tr class="saleline">  <td align="left">12</td>   <td align="left">200p CR SR 195/=</td>   <td align="right">195.00</td>   <td align="right">2,340.00</td> </tr> 
  <tr class="saleline">  <td align="left">36</td>   <td align="left">Sunlight 35g</td>   <td align="right">6.00</td>   <td align="right">216.00</td> </tr> 
              <!-- Totals -->
              <tr><td align="right" colspan="3">Subtotal</td><td align="right">2,976.00</td></tr><tr><td align="right" colspan="3"><b>TOTAL</b></td><td align="right"><b>2,976.00</b></td></tr><tr><td align="right" colspan="3">Less Payment</td><td align="right">2,760.00</td></tr><tr class="total"><td align="right" colspan="3"><strong>Balance Due</strong></td><td align="right">216.00</td></tr>
            </table></td>
        </tr>
      </table></td>
  </tr>
  <tr>
    <td><table width="100%" style="margin-top:30px">
        <tr>
          <td width="50%" class="block6"><h2></h2>
          </td>
          <td width="50%" class="block7" align="right"><h2></h2>
          </td>
        </tr>
      </table></td>
  </tr>
  <tr>
    <td><table class="block11" width="100%">
        <tr>
          <td><table></table></td>
        </tr>
      </table></td>
  </tr>
  <tr>
    <td></td>
  </tr>
</table>
  <div class="signature">
    <table border="0" cellspacing="2" cellpadding="2" align="left">
      <tr>
        <td style="padding-bottom:30px"></td>
      </tr>
      <tr>
        <td><b>Signed by:</b> 
<br/><b>Date:</b> 
<br/><b>Signature:</b><br/></td>
      </tr>
    </table>
  </div>
<div id="footer">
<table width="100%" border="0" cellpadding="2">
  <tr>
    <td align="center"><span class="footer-text">Thank you for your business.</span></td>
  </tr>
</table>
</div>
</body>
</html>

如何将HTML文件中的必要信息保存到字符串变量中

您需要一个html解析器,试试这个http://htmlagilitypack.codeplex.com/

加载页面到HtmlDocument

HtmlWeb htmlWeb = new HtmlWeb();
HtmlDocument htmlDocument = htmlWeb.Load("url");

获取指定Id表

HtmlNode table = htmlDocument.DocumentNode.Descendants("table").SingleOrDefault(x => x.Id == "tableId");

遍历节点查找值

foreach(HtmlNode child in table.ChildNodes)
{
    if (child.NodeType != HtmlNodeType.Text)
    {
        Console.WriteLine(child.Name);
    }
}

更多内容可以点击这里http://www.codeproject.com/Articles/691119/Html-Agility-Pack-Massive-information-extraction-f

必须使用jquery的parseHTML函数,然后循环遍历每个元素以获取值。下面是工作示例(可以根据您的需要进行更精细的修改)

$(document).ready(function () {
        var str = '<tr><td align="right" colspan="3">Subtotal</td><td align="right">2,976.00</td></tr><tr><td align="right" colspan="3"><b>TOTAL</b></td><td align="right"><b>2,976.00</b></td></tr><tr><td align="right" colspan="3">Less Payment</td><td align="right">2,760.00</td></tr><tr class="total"><td align="right" colspan="3"><strong>Balance Due</strong></td><td align="right">216.00</td></tr>';
        var html = $.parseHTML(str);
        $.each(html, function (index, element) {
            if ($(this).find("td:first").html() == "Subtotal")
                console.log($(this).find("td:last").html());
            else if ($(this).find("td:first b").html() == "TOTAL")
                console.log($(this).find("td:last b").html());
            else if ($(this).find("td:first").html() == "Less Payment")
                console.log($(this).find("td:last").html());
            else if ($(this).find("td:first strong").html() == "Balance Due")
                console.log($(this).find("td:last").html());
        });
    });