1

Below mentioned string is:

<table align="center" style="width: 800px;" border="0" cellspacing="1" cellpadding="1">
<tbody>
    <tr>
        <td id="litxt_1">Note : <input name="txt_1" tabindex="0" title="testTitle" disabled="disabled" id="txt_1" style="cursor: not-allowed; background-color: rgb(204, 204, 204);" type="text" size="100" value=""></td>
    </tr>
    <tr>
        <td style="text-align: center;"><u><span style="font-size: 18px;"><strong>Test Split<br>
        (Test Query)</strong></span></u></td>
    </tr>
    <tr>
        <td id="lichk_2">&nbsp;<input name="chk_2" tabindex="0" title="testTitle" disabled="disabled" id="chk_2" style="cursor: not-allowed; background-color: rgb(204, 204, 204);" type="checkbox" value="1" rel="#"></td>
    </tr>
    <tr>
        <td id="lichk_3">&nbsp;<input name="chk_3" tabindex="0" title="test Title" disabled="disabled" id="chk_3" style="cursor: not-allowed; background-color: rgb(204, 204, 204);" type="checkbox" value="1" rel="#"></td>
    </tr>
 </tbody>
</table>

now in code behind

string strAllline = File.ReadAllText("above html path");
string[] strResult = // strAllline.Split();

i am trying to get result in array strAllline.Split().Where(s => s.StartsWith("<input "))

Maksim Simkin
  • 9,561
  • 4
  • 36
  • 49
M.Y.Mnu
  • 718
  • 8
  • 22
  • 8
    Don't try to parse (or split) HTML with regex or even pure string methods. Use a html-parser like [HtmlAgilityPack](https://htmlagilitypack.codeplex.com/) – Tim Schmelter Jan 10 '17 at 10:27
  • 1
    @TimSchmelter can I ask why? just curious. – Badiparmagi Jan 10 '17 at 10:28
  • An xml parse is better able to keep parent-child relationships and to keep the array structures. – jdweng Jan 10 '17 at 10:31
  • 1
    @Badiparmagi: well, parsing valid html (or even self generated) is possible and could be solved with regex (or even string methods). But parsing arbitrary (X)HTML/CSS/DOM is impossible to parse with regex because it's not a regular language. You know [this funny, famous answer](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454)? – Tim Schmelter Jan 10 '17 at 10:44
  • @TimSchmelter thanks for the response. – Badiparmagi Jan 10 '17 at 11:24

2 Answers2

3

I am strongly agree with comment from Tim Schmelter.

With HtmlAgilityPack you could do this with 3 lines of code:

HtmlDocument doc =  new HtmlDocument();
doc.LoadHtml(t);
var strResult = doc.DocumentNode.Descendants("input").Select(d=>d.OuterHtml);
Maksim Simkin
  • 9,561
  • 4
  • 36
  • 49
1
            string[] result = renderHTML.Split('\n');
            string strInput = "";
            string strFinal = "";
            foreach (var item in result)
            {
                if (item.Contains("<input"))
                {
                    int indexOfSteam = item.IndexOf("<input");
                    strInput = item.Remove(0, indexOfSteam);
                    strInput = strInput.Replace("<td>", "");
                    strInput = strInput.Replace("</td>", "");
                    strFinal += strInput + "|";
                    strInput = "";
                }
            }
            string[] controls = strFinal.Split('|');

Above code is giving me exact result which i want, can anyone convert it into linq

M.Y.Mnu
  • 718
  • 8
  • 22
  • 1
    @Maksim Simkin & ...., suppose if we have above table in string variable? that is string variable. :-) – M.Y.Mnu Jan 13 '17 at 13:47