2011-09-26 83 views
0

我需要從ASP .NET中的文本文件中提取數據。編輯並從文本文件中提取數據(ASP .NET)

實施例的數據:

; comment 
    data = astringvalue 
    ; comment 
    ; string values 
    person = bob 
    animal = rabbit 
    ; boolean values (yes/no) 
    isValid = yes 
    isAnimal = no 

我將要創建的每個是不是一個註釋行的GUI控制。 什麼是提取每一行並確定它是字符串還是布爾值的最佳方式。 性能是必須的,因爲該文件可能相當大。

編輯:在某些時候,我將需要更新網頁中更新的值。

private void ShowConfig() 
    { 
    string configLine = String.Empty; 
using (TextReader tr = File.OpenText(@"textfile")) 
      { 
      do 
       { 
       configLine = tr.ReadLine(); 
       if (!String.IsNullOrEmpty(configLine) && !configLine.Contains(Convert.ToChar(";"))) 
        { 
        CreateControl(configLine); 
        } 
       } while (configLine != null); 
      } 

private void CreateControl(string configline) 
    { 
    string lineHeader = string.Empty; 
    string lineValue = String.Empty; 
    for (int i = 0; i < configline.Length; i++) 
     { 
     if (configline[i] == Convert.ToChar("=")) 
      { 
      lineHeader = configline.Remove(i).TrimEnd(); 
      lineValue = configline.Remove(0, ++i).TrimStart(); 
      if (GetValueType(lineValue) is CheckBox) 
       { 
       this.Panel1.Controls.Add(CreateCheckBox(lineValue, lineHeader)); 
       } 
      else 
       { 
       this.Panel1.Controls.Add(CreateLabel(lineHeader)); 
       this.Panel1.Controls.Add(CreateTextBox(lineValue, lineHeader)); 
       } 
      this.Panel1.Controls.Add(CreateNewLine()); 
      break; 
      } 
     } 
    } 

private Control GetValueType(string Value) 
    { 
    switch (Value) 
     { 
     case "yes": 
     case "no": 
      return new CheckBox(); 
     default: 
      return new TextBox(); 
     } 
    } 

將來我需要檢查更多的值類型比字符串和布爾值。

+2

你嘗試過什麼?你能發佈你的代碼並解釋你在哪裏遇到困難嗎? – Oded

+1

這是我們的作業,或者你做了什麼? – sikender

+0

數據=值屬於哪個類別?或者到底要做什麼..不清楚 – V4Vendetta

回答

2

這樣的事情呢?但是,我認爲對這種模式進行任何形式的認真的類型識別都可能是容易出錯的。爲什麼不首先使用更好的序列化?在序列化數據中捕獲類型信息的東西?

var [email protected]" ; comment 
    data = value 
    ; comment 
    ; string values 
    person = Bob 
    animal = Rabbit 
    ; boolean values (yes/no) 
    isValid = yes 
    isAnimal = no"; 

var parsed = data 
    .Split(new[]{"\r\n","\r","\n"}, StringSplitOptions.RemoveEmptyEntries) 
    .Select(line => line.Trim()) 
    .Where(line => !line.StartsWith(";")) 
    .Select(line => line.Split('=').Select(item => item.Trim())) 
    .Where(kv => kv.Count() == 2) 
    .Select(kv => new{key = kv.First(), value = kv.Last()}) 
    .Select(kv => 
     new{kv.key, kv.value, isBool = Regex.IsMatch(kv.value,"yes|no")}); 

以板載@Rubens的意見,如果數據源太大,一次加載,你可以用另外一個輔助方法的流中的數據:

static IEnumerable<string> Lines(string filename) 
{ 
    using (var sr = new StreamReader(filename)) 
    { 
     while (!sr.EndOfStream) 
     { 
      yield return sr.ReadLine(); 
     } 
    } 
} 

然後:

Lines(@"c:\path\to\data") 
    .Select(line => line.Trim()) 
    .Where(line => !line.StartsWith(";")) 
    .Select(line => line.Split('=').Select(item => item.Trim())) 
    .Where(kv => kv.Count() == 2) 
    .Select(kv => new{key = kv.First(), value = kv.Last()}) 
    .Select(kv => 
     new{kv.key, kv.value, isBool = Regex.IsMatch(kv.value,"yes|no")}); 
+0

也許你應該爲每一行運行這段代碼,因爲源文件可能「非常大」 –

0
StreamReader sr = null; 
while(!sr.EndOfStream) 
{ 
    string line = sr.ReadLine(); 
    if (string.IsNullOrEmpty(line) || line.StartsWith(";")) continue; 

    string[] tokens = line.Split("= ".ToCharArray(), 
           StringSplitOptions.RemoveEmptyEntries); 
    if(tokens.Length == 2) 
    { 
     if("Yes".Equals(tokens[1], StringComparison.CurrentCultureIgnoreCase) || 
      "No" .Equals(tokens[1], StringComparison.CurrentCultureIgnoreCase)) 
     { 
      // boolean 
     } 
     else 
     { 
      // non boolean 
     } 
    } 
}