我有一个文件,我想扫描并将这些部分放入自定义对象。
该文件如下:
[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
}
}
}
}
答案 0 :(得分:2)
你可以这样做:
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);
}
答案 1 :(得分:1)
应该让你开始的一些伪代码:
if (line != null && line.Contains("Student"))
{
var temp = new Student();
do {
string temp_line = String line = sr.ReadLine();
if //temp_line is empty
{
// you got the line. split on "="
// assign the student property to the value
// you'll have to use a bit of reflection here, but shouldn't be too hard
}
} while // not empty string (which means you're ready for the next object)
}
我可能会将它们分解为只填充每个对象的方法(学生/老师等......)
好处是您不依赖于订单,并且您可以添加将要执行的代码哦,该属性不存在,跳过它"如果你喜欢......
答案 2 :(得分:1)
您可以使用反射。喜欢这个
public static Profile readFile(string filename)
{
var profile = new Profile();
var properties = typeof(Profile).GetProperties().ToDictionary(q => q.Name, q => q);
using (StreamReader sr = new StreamReader(filename))
{
String mode = "";
while (!sr.EndOfStream)
{
String line = sr.ReadLine();
if (line == "[Student]")
{
mode = "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
}
else if (line == "[Teacher]")
{
mode = "teacher";
}
else if (!string.IsNullOrEmpty(line))
{
var nameValues = line.Split(new char[] { '=' }, 2);
if (nameValues.Length < 2)
continue;
var key = mode + nameValues[0];
if (properties.ContainsKey(key))
{
var value = nameValues[1];
properties[key].SetValue(profile, value);
}
}
}
}
return profile;
}
答案 3 :(得分:1)
你可以找到括号,如下:
class memberLocation
{
public int start, end;
}
class Program
{
static void Main(string[] args)
{
memberLocation student, teacher, manager;
student = new memberLocation();
teacher = new memberLocation();
manager = new memberLocation();
String filePath = "data.txt";
StreamReader sr = new StreamReader(filePath);
String fileData = sr.ReadToEnd();
student.start = fileData.IndexOf("[Student]");
teacher.start = fileData.IndexOf("[Teacher]");
manager.start = fileData.IndexOf("[Manager]");
student.end = fileData.IndexOf(']', student.start + 9) - 9; // [Student] length = 9 - [Teacher] length
teacher.end = fileData.IndexOf(']', teacher.start + 9) - 9; // [Teacher] length = 9
manager.end = fileData.IndexOf(']', manager.start + 9) - 9; // [Manager] length = 9
String studentStr, teacherStr, managerStr;
if (student.end > 0)
studentStr = fileData.Substring(student.start, student.end - student.start);
else
studentStr = fileData.Substring(student.start);
if (teacher.end > 0)
teacherStr = fileData.Substring(teacher.start, teacher.end - teacher.start);
else
teacherStr = fileData.Substring(teacher.start);
if (manager.end > 0)
managerStr = fileData.Substring(manager.start, manager.end - manager.start);
else
managerStr = fileData.Substring(manager.start);
}
}