我需要從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();
}
}
將來我需要檢查更多的值類型比字符串和布爾值。
你嘗試過什麼?你能發佈你的代碼並解釋你在哪裏遇到困難嗎? – Oded
這是我們的作業,或者你做了什麼? – sikender
數據=值屬於哪個類別?或者到底要做什麼..不清楚 – V4Vendetta