views:

414

answers:

2

I am using HTML Agility Pack to parse html content. I am using parsing to extract table information. It works. But if there is no ending "/tr" tag or "/td" tag then it does not parse that information perfectly.(in which there is no ending tr tag or td tag.)

Like

    <html>
  <head>
    <meta name="generator" content=
    "HTML Tidy for Windows (vers 14 February 2006), see www.w3.org">
    <title></title>
  </head>
  <body>
    <table cellspacing="0" cellpadding="0" width="100%" border="0">
      <tbody>
        <tr>
          <td class="xl27" valign="bottom" colspan="9">
            Sir / Madam,<br>
            I/We have this day done by your order and on your account the
            following transactions:
          </td>
          <td class="xl27boTRL" align="middle" colspan="5">
            Stamp duty as required under the relevant stamp act to be paid on
            consolidated basis at the end of the month.
          </td>
        </tr>
        <tr height="30">
          <td class="xl27boTBL" align="middle" width="7%">
            Order No
          </td>
          <td class="xl27boTBL" align="middle" width="4%">
            Order Time
          </td>

          <td class="xl27boTBL" align="middle" width="5%">
            Net Rate
          </td>
          <td class="xl27boTBL" align="middle" width="5%">
            Service Tax
          </td>
          <td class="xl27boTBL" align="middle" width="5%">
           Amount
          </td>
          <td class="xl27boTRBL" style="BORDER-BOTTOM: windowtext 1pt solid;"
          align="middle" width="8%">
          Net Amount Rs
          </td>
        </tr>
        <tr height="20">
          <td class="xl27boL" nowrap width="7%">
            25222105
          </td>
          <td class="xl27boL" nowrap width="4%">
            14:02:39
          </td>


          <td class="xl27boL" nowrap align="right" width="5%">
             
          </td>
          <td class="xl27boL" nowrap align="right" width="5%">
             
          </td>
          <td class="xl27boRL" nowrap align="right" width="8%">
            125288.00 
          </td>

        <tr height="20">
          <td class="xl27boL" nowrap width="7%">
            122122141
          </td>
          <td class="xl27boL" nowrap width="4%">
            14:01:56
          </td>


          <td class="xl27boL" nowrap align="right" width="5%">
             
          </td>
          <td class="xl27boL" nowrap align="right" width="5%">
             
          </td>
          <td class="xl27boRL" nowrap align="right" width="8%">
            249612.64 
          </td>

        <tr height="20">
          <td class="xl27boL" nowrap width="7%">
             
          </td>
          <td class="xl27boL" nowrap width="4%">
             
          </td>
          <td class="xl27boL" nowrap width="7%">
             
          </td>
          <td class="xl27boL" nowrap width="4%">
             
          </td>
          <td class="xl27boL" nowrap align="left" width="15%">
            [SERVICE TAX]
          </td>
          <td class="xl27boL" nowrap align="right" width="5%">
             
          </td>
          <td class="xl27boL" nowrap align="right" width="5%">
             
          </td>
          <td class="xl27boL" nowrap align="right" width="5%">
             
          </td>
          <td class="xl27boL" nowrap align="right" width="7%">
             
          </td>
          <td class="xl27boL" nowrap align="right" width="5%">
             
          </td>
          <td class="xl27boL" nowrap align="right" width="5%">
             
          </td>
          <td class="xl27boL" nowrap align="right" width="5%">
             
          </td>
          <td class="xl27boL" nowrap align="right" width="5%">
             
          </td>
          <td class="xl27boRL" nowrap align="right" width="8%">
            61.66
          </td>
        </tr>
      </tbody>
    </table>
  </body>
</html>

So for that what should I do ?

<TABLE  cellpadding=1 cellspacing=0 Width='100%'  style='border:1px solid #FFFFFF;''>
<TRAlign='middle' VAlign='bottom' Class='clsTRFontBold'>
<TD NoWrap class=clsTRFontHdr>ORDER NO</TD><TD NoWrap class=clsTRFontHdr>ORD TIME</TD>
<TD  NoWrap class=clsTRFontHdr>TRADE NO</TD><TD  NoWrap class=clsTRFontHdr>TRD TIME</TD>
<TD  NoWrap class=clsTRFontHdr ALIGN=CENTER>SCRIPNAME</TD>
<TD  NoWrap class=clsTRFontHdr>BUY/SELL</TD><TD  NoWrap class=clsTRFontHdr>QUANTITY</TD>
<TD NoWrap class=clsTRFontHdr align=right>RATE (RS)</TD>
<TD NoWrap class=clsTRFontHdr align=right>TOTAL (RS)</TD>
<TD NoWrap class=clsTRFontHdr align=right>TOT BROK (RS)</TD>
<TD NoWrap class=clsTRFontHdr align=right>SER TAX (RS)</TD>
<TD NoWrap class=clsTRFontHdr align=right>STT (RS)</TD>
<TD NoWrap class=clsTRFontHdr align=right>NET TOTAL (RS)</TD>
</TR>

<TR Class='clsTRFont'>
<TD NoWrap>2009030267182768</TD>
<TD NoWrap>10:28:11</TD><TD NoWrap>66950592</TD>
<TD NoWrap>10:28:25</TD>
<TD NoWrap>SESA GOA LTD</TD>
<TD NoWrap>BUY</TD>
<TD NoWrap ALIGN='RIGHT'>366 </TD>
<TD NoWrap ALIGN='RIGHT'>78.2000</TD>
<TD NoWrap ALIGN='RIGHT'>28621.20</TD>
<TD NoWrap ALIGN='RIGHT'>0.01</TD>
<TD NoWrap ALIGN='RIGHT'>0.00</TD><TD NoWrap ALIGN='RIGHT'>0.00</TD>
<TD NoWrap ALIGN='RIGHT'>-28621.21</TD></TR>
<!--tr tag missing-->
<TD NoWrap>2009030267182768</TD>
<TD NoWrap>10:28:11</TD><TD NoWrap>66950783</TD><TD NoWrap>10:28:27</TD>
<TD NoWrap>SESA GOA LTD</TD><TD NoWrap>BUY</TD><TD NoWrap ALIGN='RIGHT'>100 </TD>
<TD NoWrap ALIGN='RIGHT'>78.2000</TD><TD NoWrap ALIGN='RIGHT'>7820.00</TD>
<TD NoWrap ALIGN='RIGHT'>0.01</TD><TD NoWrap ALIGN='RIGHT'>0.00</TD>
<TD NoWrap ALIGN='RIGHT'>0.00</TD><TD NoWrap ALIGN='RIGHT'>-7820.01</TD>
</TR>
<!--tr tag missing-->
<TD NoWrap>2009030267182768</TD><TD NoWrap>10:28:11</TD>
<TD NoWrap>66956828</TD><TD NoWrap>10:29:39</TD><TD NoWrap>SESA GOA LTD</TD>
<TD NoWrap>BUY</TD><TD NoWrap ALIGN='RIGHT'>534 </TD>
<TD NoWrap ALIGN='RIGHT'>78.2000</TD><TD NoWrap ALIGN='RIGHT'>41758.80</TD>
<TD NoWrap ALIGN='RIGHT'>0.01</TD><TD NoWrap ALIGN='RIGHT'>0.00</TD>
<TD NoWrap ALIGN='RIGHT'>0.00</TD><TD NoWrap ALIGN='RIGHT'>-41758.81</TD>
</TR>
<!--tr tag missing-->
<TD NoWrap>2009030267510894</TD><TD NoWrap>11:06:12</TD><TD NoWrap>67137258</TD>
<TD NoWrap>11:09:24</TD><TD NoWrap>SESA GOA LTD</TD><TD NoWrap>SELL</TD>
<TD NoWrap ALIGN='RIGHT'>162 </TD><TD NoWrap ALIGN='RIGHT'>78.2500</TD>
<TD NoWrap ALIGN='RIGHT'>12676.50</TD><TD NoWrap ALIGN='RIGHT'>0.01</TD>
<TD NoWrap ALIGN='RIGHT'>0.00</TD><TD NoWrap ALIGN='RIGHT'>3.1320</TD>
<TD NoWrap ALIGN='RIGHT'>12673.36</TD></TR><TD NoWrap>2009030267510894</TD>
<TD NoWrap>11:06:12</TD><TD NoWrap>67137465</TD><TD NoWrap>11:09:28</TD>
<TD NoWrap>SESA GOA LTD</TD><TD NoWrap>SELL</TD><TD NoWrap ALIGN='RIGHT'>200 </TD>
<TD NoWrap ALIGN='RIGHT'>78.2500</TD><TD NoWrap ALIGN='RIGHT'>15650.00</TD>
<TD NoWrap ALIGN='RIGHT'>0.01</TD><TD NoWrap ALIGN='RIGHT'>0.00</TD>
<TD NoWrap ALIGN='RIGHT'>4.1010</TD><TD NoWrap ALIGN='RIGHT'>15645.89</TD>
</TR>
<!--tr tag missing-->
<TD NoWrap>2009030267510894</TD><TD NoWrap>11:06:12</TD>
<TD NoWrap>67137479</TD><TD NoWrap>11:09:28</TD><TD NoWrap>SESA GOA LTD</TD>
<TD NoWrap>SELL</TD><TD NoWrap ALIGN='RIGHT'>4 </TD>
<TD NoWrap ALIGN='RIGHT'>78.2500</TD><TD NoWrap ALIGN='RIGHT'>313.00</TD>
<TD NoWrap ALIGN='RIGHT'>0.01</TD><TD NoWrap ALIGN='RIGHT'>0.00</TD>
<TD NoWrap ALIGN='RIGHT'>0.0773</TD><TD NoWrap ALIGN='RIGHT'>312.91</TD>
</TR>
<!--tr tag missing-->
<TD NoWrap>2009030267510894</TD><TD NoWrap>11:06:12</TD><TD NoWrap>67137995</TD>
<TD NoWrap>11:09:32</TD><TD NoWrap>SESA GOA LTD</TD><TD NoWrap>SELL</TD>
<TD NoWrap ALIGN='RIGHT'>16 </TD><TD NoWrap ALIGN='RIGHT'>78.2500</TD>
<TD NoWrap ALIGN='RIGHT'>1252.00</TD><TD NoWrap ALIGN='RIGHT'>0.01</TD>
<TD NoWrap ALIGN='RIGHT'>0.00</TD><TD NoWrap ALIGN='RIGHT'>0.3093</TD>
<TD NoWrap ALIGN='RIGHT'>1251.68</TD></TR>
<!--tr tag missing-->
<TD NoWrap>2009030267510894</TD>
<TD NoWrap>11:06:12</TD><TD NoWrap>67138097</TD><TD NoWrap>11:09:34</TD>
<TD NoWrap>SESA GOA LTD</TD><TD NoWrap>SELL</TD><TD NoWrap ALIGN='RIGHT'>100 </TD>
<TD NoWrap ALIGN='RIGHT'>78.2500</TD><TD NoWrap ALIGN='RIGHT'>7825.00</TD>
<TD NoWrap ALIGN='RIGHT'>0.01</TD><TD NoWrap ALIGN='RIGHT'>0.00</TD>
<TD NoWrap ALIGN='RIGHT'>1.9333</TD><TD NoWrap ALIGN='RIGHT'>7823.06</TD>
</TR>
<!--tr tag missing-->
<TD NoWrap>2009030267510894</TD><TD NoWrap>11:06:12</TD><TD NoWrap>67138333</TD><TD NoWrap>11:09:39</TD><TD NoWrap>SESA GOA LTD</TD><TD NoWrap>SELL</TD><TD NoWrap ALIGN='RIGHT'>200 </TD><TD NoWrap ALIGN='RIGHT'>78.2500</TD><TD NoWrap ALIGN='RIGHT'>15650.00</TD><TD NoWrap ALIGN='RIGHT'>0.01</TD><TD NoWrap ALIGN='RIGHT'>0.00</TD><TD NoWrap ALIGN='RIGHT'>3.8666</TD><TD NoWrap ALIGN='RIGHT'>15646.12</TD>
</TR>
<!--tr tag missing-->
<TD NoWrap>2009030267510894</TD><TD NoWrap>11:06:12</TD><TD NoWrap>67138344</TD><TD NoWrap>11:09:40</TD><TD NoWrap>SESA GOA LTD</TD><TD NoWrap>SELL</TD><TD NoWrap ALIGN='RIGHT'>318 </TD><TD NoWrap ALIGN='RIGHT'>78.2500</TD><TD NoWrap ALIGN='RIGHT'>24883.50</TD><TD NoWrap ALIGN='RIGHT'>0.01</TD><TD NoWrap ALIGN='RIGHT'>0.00</TD><TD NoWrap ALIGN='RIGHT'>6.1479</TD><TD NoWrap ALIGN='RIGHT'>24877.34</TD>
</TR>
<!--tr tag missing-->
<TD NoWrap>2009030268222556</TD><TD NoWrap>13:03:50</TD><TD NoWrap>67511545</TD><TD NoWrap>13:03:51</TD><TD NoWrap>SESA GOA LTD</TD><TD NoWrap>BUY</TD><TD NoWrap ALIGN='RIGHT'>733 </TD><TD NoWrap ALIGN='RIGHT'>78.0000</TD><TD NoWrap ALIGN='RIGHT'>57174.00</TD><TD NoWrap ALIGN='RIGHT'>0.01</TD><TD NoWrap ALIGN='RIGHT'>0.00</TD><TD NoWrap ALIGN='RIGHT'>0.00</TD><TD NoWrap ALIGN='RIGHT'>-57174.01</TD>
</TR>
<!--tr tag missing-->
<TD NoWrap>2009030268222556</TD><TD NoWrap>13:03:50</TD><TD NoWrap>67511621</TD><TD NoWrap>13:03:53</TD><TD NoWrap>SESA GOA LTD</TD><TD NoWrap>BUY</TD><TD NoWrap ALIGN='RIGHT'>2 </TD><TD NoWrap ALIGN='RIGHT'>78.0000</TD><TD NoWrap ALIGN='RIGHT'>156.00</TD><TD NoWrap ALIGN='RIGHT'>0.01</TD><TD NoWrap ALIGN='RIGHT'>0.00</TD><TD NoWrap ALIGN='RIGHT'>0.00</TD><TD NoWrap ALIGN='RIGHT'>-156.01</TD>
</TR>
<!--tr tag missing-->
<TD NoWrap>2009030268222556</TD><TD NoWrap>13:03:50</TD><TD NoWrap>67511797</TD><TD NoWrap>13:03:58</TD><TD NoWrap>SESA GOA LTD</TD><TD NoWrap>BUY</TD><TD NoWrap ALIGN='RIGHT'>1 </TD><TD NoWrap ALIGN='RIGHT'>78.0000</TD><TD NoWrap ALIGN='RIGHT'>78.00</TD><TD NoWrap ALIGN='RIGHT'>0.01</TD><TD NoWrap ALIGN='RIGHT'>0.00</TD><TD NoWrap ALIGN='RIGHT'>0.00</TD><TD NoWrap ALIGN='RIGHT'>-78.01</TD>
</TR>
<!--tr tag missing-->
<TD NoWrap>2009030268222556</TD><TD NoWrap>13:03:50</TD><TD NoWrap>67512082</TD><TD NoWrap>13:04:05</TD><TD NoWrap>SESA GOA LTD</TD><TD NoWrap>BUY</TD><TD NoWrap ALIGN='RIGHT'>264 </TD><TD NoWrap ALIGN='RIGHT'>78.0000</TD><TD NoWrap ALIGN='RIGHT'>20592.00</TD><TD NoWrap ALIGN='RIGHT'>0.01</TD><TD NoWrap ALIGN='RIGHT'>0.00</TD><TD NoWrap ALIGN='RIGHT'>0.00</TD><TD NoWrap ALIGN='RIGHT'>-20592.01</TD>
</TR>
<!--tr tag missing-->
<TD NoWrap>2009030268378000</TD><TD NoWrap>13:31:04</TD><TD NoWrap>67609079</TD><TD NoWrap>13:33:39</TD><TD NoWrap>SESA GOA LTD</TD><TD NoWrap>BUY</TD><TD NoWrap ALIGN='RIGHT'>405 </TD><TD NoWrap ALIGN='RIGHT'>77.6000</TD><TD NoWrap ALIGN='RIGHT'>31428.00</TD><TD NoWrap ALIGN='RIGHT'>0.01</TD><TD NoWrap ALIGN='RIGHT'>0.00</TD><TD NoWrap ALIGN='RIGHT'>0.00</TD><TD NoWrap ALIGN='RIGHT'>-31428.01</TD>
</TR>
<!--tr tag missing-->
<TD NoWrap>2009030268378000</TD><TD NoWrap>13:31:04</TD><TD NoWrap>67609374</TD><TD NoWrap>13:33:46</TD><TD NoWrap>SESA GOA LTD</TD><TD NoWrap>BUY</TD><TD NoWrap ALIGN='RIGHT'>45 </TD><TD NoWrap ALIGN='RIGHT'>77.6000</TD><TD NoWrap ALIGN='RIGHT'>3492.00</TD><TD NoWrap ALIGN='RIGHT'>0.01</TD><TD NoWrap ALIGN='RIGHT'>0.00</TD><TD NoWrap ALIGN='RIGHT'>0.00</TD><TD NoWrap ALIGN='RIGHT'>-3492.01</TD>
</TR>
<!--tr tag missing-->
<TD NoWrap>2009030268779359</TD><TD NoWrap>14:32:04</TD><TD NoWrap>67870192</TD><TD NoWrap>14:32:41</TD><TD NoWrap>SESA GOA LTD</TD><TD NoWrap>BUY</TD><TD NoWrap ALIGN='RIGHT'>900 </TD><TD NoWrap ALIGN='RIGHT'>77.3000</TD><TD NoWrap ALIGN='RIGHT'>69570.00</TD><TD NoWrap ALIGN='RIGHT'>0.01</TD><TD NoWrap ALIGN='RIGHT'>0.00</TD><TD NoWrap ALIGN='RIGHT'>0.00</TD><TD NoWrap ALIGN='RIGHT'>-69570.01</TD>
</TR>
<!--tr tag missing-->
<TD NoWrap>2009030269013760</TD><TD NoWrap>15:03:56</TD><TD NoWrap>68018179</TD><TD NoWrap>15:03:56</TD><TD NoWrap>SESA GOA LTD</TD><TD NoWrap>SELL</TD><TD NoWrap ALIGN='RIGHT'>146 </TD><TD NoWrap ALIGN='RIGHT'>76.2500</TD><TD NoWrap ALIGN='RIGHT'>11132.50</TD><TD NoWrap ALIGN='RIGHT'>0.01</TD><TD NoWrap ALIGN='RIGHT'>0.00</TD><TD NoWrap ALIGN='RIGHT'>2.8226</TD><TD NoWrap ALIGN='RIGHT'>11129.67</TD>
</TR>
<!--tr tag missing-->
<TD NoWrap>2009030269013760</TD><TD NoWrap>15:03:56</TD><TD NoWrap>68018180</TD><TD NoWrap>15:03:56</TD><TD NoWrap>SESA GOA LTD</TD><TD NoWrap>SELL</TD><TD NoWrap ALIGN='RIGHT'>10 </TD><TD NoWrap ALIGN='RIGHT'>76.2500</TD><TD NoWrap ALIGN='RIGHT'>762.50</TD><TD NoWrap ALIGN='RIGHT'>0.01</TD><TD NoWrap ALIGN='RIGHT'>0.00</TD><TD NoWrap ALIGN='RIGHT'>0.1933</TD><TD NoWrap ALIGN='RIGHT'>762.30</TD>
</TR>
<TABLE cellpadding=0 cellspacing=0 border=0><br>
A: 

I haven't tried this, but does it help to add this code?

HtmlNode.ElementsFlags.Add("tr", HtmlElementFlag.Closed);
HtmlNode.ElementsFlags.Add("td", HtmlElementFlag.Closed);
Jeremy Stein
@Jeremy Stein..It adds end tags but not at the appropriate location If there is a table like `<tr><td>firstrow</td> <tr><td>secondrow</td>` then and we do like `HtmlNode.ElementsFlags.Add("tr",HtmlElementFlag.Closed);` then the table will `<tr></tr><td>firstrow</td> <tr></tr><td>secondrow</td>` instead of `<tr><td>firstrow</td></tr> <tr><td>secondrow</td></tr>`
Harikrishna
Thanks for following up. Sorry I wasted your time on that idea.
Jeremy Stein
@Jeremy Stein..It's ok.
Harikrishna
+1  A: 

Since you tested my other idea and it didn't work, I think you have only two options:

  1. Modify HTML Agility Pack to handle your case, or
  2. Fill in the missing </tr>s yourself.

Here's a regex that might fill in the missing </tr>s for you:

html = Regex.Replace(html, "<tr[^>]*>(?:(?!</?tr>|</tbody>|</table>).)*?(?=<tr[^>]*>|</tbody>|</table>)", "$&</tr>", RegexOptions.Singleline | RegexOptions.IgnoreCase);

(If someone can improve my regex, please feel free.)

Jeremy Stein
@Jeremy Stein..Thanks for the answer..Above regex works for small table but does not work for another table like I have put the table in the question which I had changed.
Harikrishna
OK, I edited my answer to allow attributes on the `tr` tag.
Jeremy Stein
@Jeremy Stein.Thanks for the answer.Above regex works for that but I found another table in which this regex does not work.So I found html tidy as a solution.Thank you very much again for helping..
Harikrishna
@Jeremy Stein,I was using html tidy pack and it inserts the missing closing tag but it also does other formatting what I don't want.I want to insert the missing tag only in the html file where necessary.So I want to use regex for that because only regex will fulfill my need but regex does not insert the </tr> for above table which I have put in the question.
Harikrishna
@Jeremy Stein,Thank You Very Much for helping..Your regex is very good and works very good...
Harikrishna
@Jeremy Stein,If the starting tag is missing then which regex should be there ?
Harikrishna
Give me an example.
Jeremy Stein
http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454
RCIX
@Jeremy Stein,I have update my question with that example in which opeing tr tag is missing
Harikrishna
It's unreasonable to expect to be able to parse such invalid HTML. I suggest you switch to a regex to pull the data you need.
Jeremy Stein
@Jeremy Stein,It's good feeling to get you back after many days,because I have this problem and I can not get it solved.But I can not switch to regex because it is almost to complete that project with parser and in only one file there is a problem like that.And html file is displayed properly of example which with I had updated my question which has missing opening tag.So if it is possible I want to use here regex to insert missing opening tags.
Harikrishna
@Jeremy Stein, Is it possible with regex ?
Harikrishna
Sure. Download the evaluation version of RegexBuddy, copy in your HTML, and tweak a regex until it works.
Jeremy Stein
@Jeremy Stein, Will you please help to make the regex for inserting opening tags in html file ?
Harikrishna
You may want to do something like this: `html = Regex.Replace(html, "(?<=</tr>[^<]*)<td", "<tr>$`
Jeremy Stein