将对象从文本文件添加到列表

马蒂亚斯·利马(Matias Lima)

这是我的代码:它仅添加文本包含的.txt中的第一个1(Roberto):

1,2343443,罗伯托,洛佩兹

2,3434343,何塞,佩雷斯

3,12123242,Dario,Gimenez

0.45789432,豪尔赫,洛佩兹

[HttpPost]
public string CargarAlumnos()
{
    List<Alumno> lstAlumnos = new List<Alumno>();
    if (Request.Files.Count > 0)
    {
        var file = Request.Files[0];

        if (file != null && file.ContentLength > 0)
        {
            StreamReader reader = new StreamReader(file.InputStream);
            do
            {
                string textLine = reader.ReadLine();
                textLine.Replace("<br>", "");
                String[] dato = textLine.Split(',');
                while (textLine != null)
                {
                        Alumno nAlu = new Alumno
                        {
                            numero = Convert.ToInt32(dato[0]),
                            cedula = dato[1],
                            nombre = dato[2],
                            apellido = dato[3]
                        };
                        lstAlumnos.Add(nAlu);
                    textLine = reader.ReadLine();
                }reader.Close();
            }while (reader.Peek() != -1);

            return "Alumnos Cargados";
        }
    }

    return "No se pudo procesar el archivo";
}
高朗戴夫

您采用了循环,这不必要地使逻辑变得复杂。以下代码对于读取文本文件非常简单。经过测试和工作。

[HttpPost]
public string CargarAlumnos()
{
    List<Alumno> lstAlumnos = new List<Alumno>();
    if (Request.Files.Count > 0)
    {
        var file = Request.Files[0];

        if (file != null && file.ContentLength > 0)
        {
            StreamReader reader = new StreamReader(file.InputStream);

            while (!reader.EndOfStream) 
            { 
                var line = reader.ReadLine(); 
                line = line.Replace("<br>", ""); 
                string[] data = line.Split(','); 

                if (data != null && data.Count() > 0) 
                { 
                    // YOUR DATA IS HERE 
                    Alumno nAlu = new Alumno
                    {
                        numero = Convert.ToInt32(data[0]),
                        cedula = data[1],
                        nombre = data[2],
                        apellido = data[3]
                    };

                    lstAlumnos.Add(nAlu);
                }
            }   
        }
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

阅读文本文件并添加到列表

将对象列表写入文本文件

将文本文件内容添加到列表列表中

将文本添加到文本文件的开头

遍历文本文件中的每一行并添加到列表框

从文本文件中读取行并将其添加到列表框

C:将数据从文本文件添加到单链列表中

拆分文本文件条目以添加到数组列表

将文本文件中的双打添加到数组列表中

将所有类对象数组值添加到文本文件

Applescript,添加到文本文件的底部

将 JSON 数组添加到文本文件

将特定行添加到文本文件

从文本文件添加到列表视图的第一项只是添加了两次

如何将用户输入的号码添加到我的文本文件列表中?

导入、读取和添加到保存在文本文件中的列表的最佳方法是什么?

类似于一个较早的问题“将标题添加到列表文本文件中的列”

将文本文件每一行中的每个字符添加到 Python 列表中

如何从文本文件中提取特定的字符串并将其添加到列表中?

将对象添加到链接列表

将对象添加到列表

将对象添加到通用列表

将对象添加到列表

如何将文本文件添加到 bash 脚本中,然后使用该文本文件

将文本添加到文本文件的开头,而不必在R中复制整个文件

将img标签添加到文本区域以输入文本文件

Bash:使用awk将文本从文本文件添加到变量

从文本文件读取要添加到标签的文本时,在标签中换行

将.pdf添加到文本文件中的文本末尾-bash