I have the following file :

AD20100428 0000250000000000
AD20100429 0000014521201000
AD20100501 0000000037600000
AD20100501 0000004892201000
AD20100421 0000005000001000
AD20100512 0000002000001575
AD20100514 0000015000000800
AD20100426 S0000039566700000
AD20100519 0000002892201350

AH denotes an account header (the number refers to a specific account) while AD refers to the detail of that specific AH. I need the code to loop from one instance of AH to the next while processing the AD in between.

So basically I would have three sets of data as follows :

I've tried various iterations of indexof, as well as foreach loops and arrays but I'm completely stumped.

Your problem is not so clear so If you attach your code as well and explain little bit more what you trying to do with your code, may be some one help you

Edited 6 Years Ago by prvnkmr449: n/a

ok lets say that we have 5 lines in a text file

A Boys
B Adam
B Steve
A Girls
B Anne

I need the code to come to the first "A" in the textfile and then create a list of "Boys" with "Adam" and "Steve" under that heading
Then it needs to terminate once it gets to the next instance of "A" and create another list called "Girls" populated with "Anne".

So we would have :
Boys Girls
Adam Anne

I didn't post any code because I haven't been able to get anywhere near a solution.

public Dictionary<String, List<String>> FileParser(String fileName) {
    Dictionary<String, List<String>> myDictionary = new Dictionary<string,List<string>>();
    using (StreamReader sr = new StreamReader(fileName)) {
        List<String> temp = null;
        String index = null;
        String line;

        while ((line = sr.ReadLine()) != null) {
            if (line.StartsWith("AH")) {
                if (temp != null) {
                    myDictionary.Add(index, temp);
                index = line.Substring(2);
                temp = new List<string>();
            } else {

        myDictionary.Add(index, temp);

    return myDictionary;

What this code does is sets up storage for the data. Then we go through each line in the file. If we find an AH, we save any previous AH/AD information we've gathered, then create a new AH (index) and storage for the AD values (temp). We continue through the file until we've read the last line. At that point, we have an AH with the ADs for it, but haven't saved them so we do so (line 20).

WARNING: There is no error checking in this code. A line is either an AH or an AD. Blank lines will cause this to explode. Wrong filename will cause an error. If the first line isn't an AH bad things will happen. Duplicate AH values will cause issues (duplicate AD values will not, even when assigned to the same AH).

This article has been dead for over six months. Start a new discussion instead.