class Program
{
static void Main(string[] args)
{
string[] data = File.ReadAllLines("data.txt");
List<Persona> output = new List<Persona>();
char[] delimiters = { ';' };
foreach (string line in data)
{
Persona tmp = new Persona();
string[] parsedLine = line.Split(delimiters);
tmp.Name = parsedLine[0];
int.TryParse(parsedLine[1], out tmp.Age);
int.TryParse(parsedLine[2], out tmp.PartnerAge);
int.TryParse(parsedLine[3], out tmp.Height);
tmp.Sex = parsedLine[4];
output.Add(tmp);
}
foreach (Persona singlePerson in output)
{
Console.Write(singlePerson.ToString());
Console.WriteLine("---------");
}
Console.ReadKey();
}
}
public class Persona
{
public string Name;
public int Age;
public int PartnerAge;
public int Height;
public string Sex;
public Persona(): this("N/A", -1, -1, -1, "N/A") { }
public Persona(string name, int age, int partnerAge, int height, string sex)
{
this.Name = name;
this.Age = age;
this.PartnerAge = partnerAge;
this.Height = height;
this.Sex = sex;
}
public override string ToString()
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("<PERSONA>");
sb.AppendLine("Name: " + this.Name);
sb.AppendLine("Age: " + this.Age.ToString());
sb.AppendLine("Partner Age: " + this.PartnerAge.ToString());
sb.AppendLine("Height (cm): " + this.Height.ToString());
sb.AppendLine("Sex (K/M): " + this.Sex.ToString());
return sb.ToString();
}
}
Trochę na chama wyciągnąłem z programu, ale wiadomo o co chodzi. Przejrzyj statyczne metody klasy File, one oferują pełen zakres możliwości wczytywania danych. A poza tym metoda Split od stringa jest bardzo dobra jeśli masz separator w danej linijce i chcesz rozbić string na array stringów oddzielanych separatorem.
Zawartość pliku data.txt to dokładnie to, co podawałeś. Ten przykład jest dość sztywny, tylko na tę okazję.