Reading in a Block of Text [C#/CS]

Shaitan00

Well-known member
Joined
Aug 11, 2003
Messages
343
Location
Hell
Simply put, I have a Text File that is delimited by sections such as:
<Section1>
... information ...
... information ...
... information ...
</Section1>
(looks xml-ish but it is a simple .txt text file)

Now I am trying to write a function that will return all the information from within a section. Currently I read-in the file line-by-line (StreamReader and .ReadLine) using while loops to ensure I am within the correct section...
Code:
if (sReadLine.IndexOf("<" + sSectionName + ">") != -1)
{
	sReadLine = sr.ReadLine();    // Read next line

	while (sReadLine.IndexOf("</" + sSectionName + ">") == -1)
	{
	... read in the information ...
	}
}

So, essentially I need a way to GET all the information in between the section boundaries, question is HOW? (my way of using .ReadLine to read-in line-by-line really sucks and is ugly, just makes one LONG string that I need to split after... got to be a better way).

I was looking into .ReadBlock but I dont know how to find the "Positions" of both section headers nor do I know the size of the buffer (char[]) needed to fill in all the information (I have no clue how large each section will be)...

I want my function to read something that the rest of my application can use, like a string[] where each index represents a line within the section? Or a char[] could work also, etc... Anything will do....

Any ideas, hints, and help would be greatly appreciated, thanks
 
Is there any reason your using an xml-ish style rather than just using xml. The XmlTextReader has built in parsers for doing such tasks, but will obviously require a valid xml structure.
 
Back
Top