[英]Editing and Extracting data from a text file (ASP .NET)
I need to extract data from a text file in ASP .NET. 我需要从ASP .NET中的文本文件中提取数据。
Example Data: 示例数据:
; comment
data = astringvalue
; comment
; string values
person = bob
animal = rabbit
; boolean values (yes / no)
isValid = yes
isAnimal = no
I will be creating a GUI control for each line that is not a comment. 我将为每个不是注释的行创建GUI控件。 What is the best way to extract each line and determine if it is a string or a Boolean value. 提取每一行并确定它是字符串还是布尔值的最佳方法是什么。 Performance is a must as the file can be quite large. 性能是必须的,因为文件可能非常大。
Edit: At some point i will have need to update the values with the updated ones from the web page. 编辑:在某些时候,我需要使用网页上更新的值更新值。
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();
}
}
In the future i will need to check for more value types than string and boolean. 在将来,我将需要检查比string和boolean更多的值类型。
How about something like this? 这样的事怎么样? However, I think that working any kind of serious type-recognition into this schema is likely to be error prone. 但是,我认为对此模式进行任何类型的严重类型识别都可能容易出错。 Why not use a better serialization in the first place? 为什么不首先使用更好的序列化? Something that captures type info in the serialized data? 什么能捕获序列化数据中的类型信息?
var data=@" ; 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")});
Taking @Rubens' comments on-board, if the data source is too large to load in at once, you could stream the data with the addition of a helper method: 在板上记录@Rubens的注释,如果数据源太大而无法立即加载,您可以通过添加辅助方法来传输数据:
static IEnumerable<string> Lines(string filename)
{
using (var sr = new StreamReader(filename))
{
while (!sr.EndOfStream)
{
yield return sr.ReadLine();
}
}
}
then: 然后:
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")});
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
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.