使用c#从文本文件中删除备用行

Vik*_*war 5 c# asp.net

我正在做一个项目,我必须从.txt文件中读取数据,然后使用查询将每行插入表中.

现在例如文本文件的内容将是

11111

1111x

22222

2222x

33333

3333X

等等.

现在你可以看到备用行几乎是重复的,所以我想删除备用行,以便可用数据变为

11111

22222

33333

然后处理我的其余代码.

有什么方法可以做到吗?

到目前为止,我一直在使用数组列表来获取此信息

using (StreamReader sr = new StreamReader(Server.MapPath("03122013114450.txt"), true))
        {

            string txtValues = sr.ReadToEnd();
            string[] txtValuesArray1 = Regex.Split(txtValues, "\r\n");



            ArrayList array = new ArrayList();
            foreach (string value in txtValuesArray1)
            {
                array.Add(value);
            }

            for (int i = 0; i < array.Count; i++)
            {
                if (array.Count % 2 != 0)
                    array.RemoveAt(i + 2);
                else
                    array.RemoveAt(i + 1);
            }
        }
Run Code Online (Sandbox Code Playgroud)

基本思想是从文本文件的arraylist索引中删除备用行.

Rik*_*Rik 1

另一个优化:

using (var sr = new StreamReader(Server.MapPath("03122013114450.txt"), true))
{
    var line = sr.ReadLine();
    while (line != null)
    {
        // Do stuff here. Add to the list, maybe?

        if (sr.ReadLine()!= null) //read the next line and ignore it.
            line = sr.ReadLine();
    }
}
Run Code Online (Sandbox Code Playgroud)

如果要忽略奇数行而不是偶数行,请将line = sr.ReadLine();while 循环从末尾移动到开头