There are a lot of ways to accomplish this. All of them involve iterating through the lines of your text file.
Here's one such solution that plays on the new ValueTuple
type available in C# 7.
string path = "file path here";
Dictionary<string, (string Gender, string Weight, string BloodType)> records =
new Dictionary<string, (string Gender, string Weight, string BloodType)>();
Stack<string> stack =
new Stack<string>();
foreach (string line in File.ReadLines(path))
{
if (stack.Count != 1)
{
stack.Push(line);
continue;
}
string[] fields =
line.Split('*');
records.Add(
stack.Pop(),
(Gender: fields[0],
Weight: fields[1],
BloodType: fields[2]));
}
This snippet streams lines from the file one at a time. First it pushes the name
line onto a stack. Once there's a name on the stack, the next loop pops it off, parses the current line for record information, and adds it all to the records
dictionary using the name
as a key.
While this solution will get you started, there are some obvious areas in which you can improve it's robustness with some insight into your data environment.
For example, this solution doesn't handle cases where either the name or the record information may be missing, nor does it handle the case where the record information may not have all three fields.
You should think carefully about how to handle such cases in your implementing code.