问题描述
txt文件的例子线路胆线进入歌手名单,其中1个歌手有歌曲列表..来自Singer.txt ..
John | Newman |歌手| 09112000 |男| song1; song2; song3
Ezrea | Leusag |歌手| 09112020 |男| song1; song2; song3
List< ;歌手>
级歌手
姓名
姓氏
歌手
DateOfBirth
Gendre
列表< songs>
---歌曲对象有属性SongName
在某种程度上我需要将此行填充到List< singer>谁有名单< songs>
我尝试过:
example of txt file Line bile line into list of singers which 1 singer have list of songs..From Singer.txt..
John|Newman|Singer|09112000|Male|song1;song2;song3
Ezrea|Leusag|Singer|09112020|Male|song1;song2;song3
List<singer>
class Singer
Name
Surname
Singer
DateOfBirth
Gendre
List<songs>
---Songs object have property SongName
in one way i need to fullfill this line into List<singer> who have list<songs>
What I have tried:
using (StreamReader str = new StreamReader(@"C:\Singer.txt"))
{
char[] delimiterChars = { '|', ';'};
while ((line = str.ReadLine()) != null)
{
string[] word = line.Split(delimiterChars);
listSinger.Add(new Singer(
int.Parse(
word[0]),
word[1],
word[2],
word[3],
DateTime.ParseExact(word[4], "ddmmyyyy", CultureInfo.InvariantCulture)));
}
}
推荐答案
John|Newman|Singer|09112000|Male|song1;song2;song3
Ezrea|Leusag|Singer|09112020|Male|song1;song2;song3
你的
0 =名字,猜测,应该是字符串
1 =姓氏,猜测,应该是字符串
2 =职业(猜测),应该是字符串
3 =零填充日期,猜测,ddmmyyyy格式化8字节,应该是字符串。
4 =性别,应该是字符串
5 =分号分隔歌曲标题,再次猜测,应该是字符串
我可以看到一个构造函数,看起来像
your
0=first name, guessing, should be string
1=last name, guessing, should be string
2=Occupation(guessing), should be string
3=zero padded date, guessing, ddmmyyyy formatted 8 bytes, should be string.
4=Sex, Should be string
5=semicolon separated song title, again guessing, should be string
I can see a constructor, that looks like
new Singer(int, string, string, string, DateTime)
在这里你正在尝试解析名字到int
索引4看起来像性别(男/女),你试图解析为DateTime
以上问题应该抛出异常。
我希望你能猜出你需要做什么
Here you are trying to parse first name to int
index 4 looks like Sex(Male/Female), you are trying to parse as DateTime
for both of these above issues exception should be thrown.
I hope you can guess from this what you needed to do
public class Singer
{
private string sFName = string.Empty;
private string sSurname = string.Empty;
private string sRole = string.Empty;
private DateTime dDateOfBirth = new DateTime(1901, 1, 1); //default value
private string sGenre = string.Empty;
private List<string> songs = new List<string>();
//instantiate singer from single line
public Singer(string line)
{
string[] parts = line.Split(new string[]{"|"}, StringSplitOptions.RemoveEmptyEntries);
sFName = parts[0];
sSurname = parts[1];
sRole = parts[2];
dDateOfBirth = DateTime.ParseExact(parts[3], "ddMMyyyy", System.Globalization.CultureInfo.InvariantCulture);
sGenre = parts[4];
songs = parts[5].Split(new string[]{";"}, StringSplitOptions.RemoveEmptyEntries).ToList();
}
//instantiate singer from parts
public Singer(string _FName, string _Surname, string _Role, DateTime _DOB, string _Genre, List<string> _songs)
{
sFName = _FName;
sSurname = _Surname;
sRole = _Role;
dDateOfBirth = _DOB;
sGenre = _Genre;
songs = _songs;
}
public string FirstName
{
get { return sFName; }
set { sFName = value; }
}
public string Surname
{
get { return sSurname; }
set { sSurname = value; }
}
public string Role
{
get { return sRole; }
set { sRole = value; }
}
public DateTime DateOfBirth
{
get { return dDateOfBirth; }
set { dDateOfBirth = value; }
}
public string Genre
{
get { return sGenre; }
set { sGenre = value; }
}
public List<string> Songs
{
get { return songs; }
set { songs = value; }
}
}
用法:
Usage:
string[] lines = File.ReadAllLines("FullFileName.txt");
List<Singer> singers = lines.Select(s=> new Singer(s)).ToList();
foreach(Singer s in singers)
{
Console.WriteLine("{0} {1}, count of songs: {2}", s.FirstName, s.Surname, s.Songs.Count);
}
结果:
Result:
John Newman, count of songs: 3
Ezrea Leusag, count of songs: 3
这篇关于需要读取txt行结束导出到对象列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!