如何将文件的一部分读入对象

撇号

我有一个文件,我想扫描该文件并将其放入自定义对象中。

该文件如下所示:

[Student]
Name=John
LastName=Doe 
Username=user1
Password=pass1

[Teacher]
Name=Jane
LastName=Doe
Username=user2
Password=pass2

[Manager]
Name=Jason
LastName=Doe
Username=user3
Password=pass3

我有一个Profile看起来像这样课:

public class Profile
{
    public string studentName       { get; set; }
    public string studentLastName   { get; set; }
    public string studentUsername   { get; set; }
    public string studentPassword   { get; set; }
    public string teacherName       { get; set; }
    public string teacherLastName   { get; set; }
    public string teacherUsername   { get; set; }
    public string teacherPassword   { get; set; }
}

因此,我只需要让学生和老师进入个人资料对象即可。这是我的开始方式:

public Profile readFile(string filename)
{
     var profile = new Profile();

     using (StreamReader sr = new StreamReader(filename))
     {
         while (!sr.EndOfStream)
         {
             String line = sr.ReadLine();
             if (line != null && line.Contains("Student"))
             {
                 // and I am stuck here, not sure how to find 
                 // the next lines, so i can take the values
                 // and put them in the profile 
              }

              if (line != null && line.Contains("Teacher"))
              {
                 // and I am stuck here, not sure how to find 
                 // the next lines, so i can take the values
                 // and put them in the profile 
              }
          }
      }
  }
Arion

您可以执行以下操作:

var p=new Profile();
using (StreamReader sr = new StreamReader(filename))
{
    while (!sr.EndOfStream)
    {
        String line = sr.ReadLine();
        if (line != null && line.Contains("Student"))
        {
            p.studentName=GetLineValue(sr.ReadLine());
            p.studentLastName=GetLineValue(sr.ReadLine());
            p.studentUsername=GetLineValue(sr.ReadLine());
            p.studentPassword=GetLineValue(sr.ReadLine());
        }

        if (line != null && line.Contains("Teacher"))
        {
            p.teacherName=GetLineValue(sr.ReadLine());
            p.teacherLastName=GetLineValue(sr.ReadLine());
            p.teacherUsername=GetLineValue(sr.ReadLine());
            p.teacherPassword=GetLineValue(sr.ReadLine());
        }
    }
}
private string GetLineValue(string line)
{
   return line.Substring(line.IndexOf('=')+1);
}

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

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

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何将Java源文件的一部分转换为Kotlin?

Golang-如何将XML文件的一部分提取为字符串?

Powershell:将文件的一部分读入变量

如何将背景色仅作为选择的一部分?的CSS

如何将文本文件的内容添加为ELF文件中的一部分?

如何将一个UIViewController作为屏幕的一部分

如何将视图控制器呈现为视图的一部分?

如何将熊猫数据框按自身的一部分划分?

应该如何将数据推送到数组中已过滤对象的一部分?

sql如何将增量显示为总增量的一部分

如何将变量用作数组名称的一部分

如何将字节数组写入现有文件的一部分?

如何将图像放置在其他图像的一部分上

UNIX如何将输入文件的基础用作输出文件的一部分

如何将存储库的一部分复制到Artifactory?

如何将一部分 Python 代码保存在文本文件中?

如何将连字符视为整个单词的一部分

如何将磁盘的一部分“分配”给文件夹?

如何将 zip 文件编码为 multipart/form-data 的一部分

如何将文件的一部分读入 perl 数组并查询第二个文件(最佳实践)

如何将json的一部分存储到另一个json文件中

如何将 List<B> 添加到对象 A,其中 List<B> 是类 A 的一部分

如何将字符串的一部分更改为 JSX 元素?

如何将文件名的一部分转换为文件扩展名?

如何将单引号转义为别名复合命令的一部分?

如何将 javascript 对象的一部分存储到变量中?

如何将 Conda 命令的一部分换行?

如何将 Excel 文件名的一部分存储在 Python 整数变量中

作为函数的一部分,如何将小数转换为等效时间?