2017-01-10 3 views
1

下記の文字列は次のとおりです。がどのようにC#でLINQを使用して文字列を分割する

コードで今
<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> 

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

の後ろに、私は、配列の結果を取得しようとしています strAllline.Split().Where(s => s.StartsWith("<input "))

+8

に変換することができないでregexや純粋な文字列メソッドでもHTMLを解析(または分割)してみてください。 [HtmlAgilityPack](https://htmlagilitypack.codeplex.com/)のようなhtml-parserを使用してください –

+1

@TimSchmelter私はなぜ尋ねることができますか?ちょっと興味があるんだけど。 – Badiparmagi

+0

xml解析は、親子関係を保ち、配列構造を保つことができます。 – jdweng

答えて

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

は、私が欲しいの正確な結果を与えている、誰もがLINQ

+1

@Maksim Simkin&....上記のテーブルが文字列変数にあるとしますか?それは文字列変数です。 :-) –

3

私はTim Schmelterのコメントに強く同意します。あなたは、コードの3行でこれを行うことができHtmlAgilityPackで

:上記のコード

HtmlDocument doc = new HtmlDocument(); 
doc.LoadHtml(t); 
var strResult = doc.DocumentNode.Descendants("input").Select(d=>d.OuterHtml); 
関連する問題