Divide a Text Document into Sections using c#
<p>I'm parsing a text file that has a semi-known repeating structure. There is a heading (1 line), a sub-heading(1 line or 2 lines), and a content area (arbitrary # of lines).
The format for each item in the document is shown below:</p>

<pre> 
=========================
       Head Text 1
=========================
      SubHead Text1
      SubHead Text2
=========================
 Content Text Line 1
 Content Text Line 2
 ...
 Content Text Line 8

=========================
       Head Text 2
=========================
      SubHead Text1
      SubHead Text2
=========================
 Content Text Line 1
 Content Text Line 2
 ...
 Content Text Line 6
</pre>

<p>I would like each section to be inside a unique object, each with 3 sections... somethign like </p>

<pre>
section1.head
section1.subHead
section1.content

section2.head
section2.subHead
section2.content
</pre>

<p>The only way I can think of accomplishing this involves a lot of <code>if</code> and <code>while</code> statements. Is there an efficient way of accomplishing this?</p>

<p>I originally tried writing some code in JScipt, but I'm reading a RTF file and C# provides an easy way of converting RTF to plain text. It didn't work very well, I kept skipping some dividers and would get an error at the end of the file.</p>

<pre><code>page = new Array();

fso = new ActiveXObject("Scripting.FileSystemObject");
f = fso.GetFile("test.rtf");

is = f.OpenAsTextStream( forReading, -2 );

var count = 0;
while( !is.AtEndOfStream ){
   page[count] = is.ReadLine();
   count++; ;
}

is.Close();

WScript.Echo( page[0].text);

var item = [];

var section = 0;

var i = 0, k = 0;
while (i &lt; page.length) {
item[k] = {};

    if (!page[i].indexOf("=====")) { 
        i++;    
        item[k].head = page[i];
        i+=2;
        while(page[i].indexOf("=====")) {   // WScript.Echo( "index = " + i + " "+ page[i] +"\n" + "Next index = " + (i+1) + " "+ page[i+1] +"\n" );
            item[k].subHead += page[i];
            i++;
        }

        k++;

    } 
    i++;
}
</code></pre>
