繁体   English   中英

编辑和从文本文件中提取数据(ASP .NET)

[英]Editing and Extracting data from a text file (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();
        }
    }

在将来,我将需要检查比string和boolean更多的值类型。

这样的事怎么样? 但是,我认为对此模式进行任何类型的严重类型识别都可能容易出错。 为什么不首先使用更好的序列化? 什么能捕获序列化数据中的类型信息?

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")});

在板上记录@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")});
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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM