Jon
Wed Oct 22 03:09:32 CDT 2003
John <anonymous@discussions.microsoft.com> wrote:
> Posting to my own thread. I found that the following works
> But I wonder if I should do it in a different way or if
> this is the fastest.
Assuming the start/end tags are always at the start of the line, this
is a reasonable approach. A few suggestions though:
Do you really need to open the stream for read/write access? I can't
see why you'd need to do this immediately. If not, I'd suggest just
using:
Response.Write ("<html>");
Response.Write ("<body>");
using (StreamReader reader = new StreamReader (st))
{
String line;
boolean writing = false;
while ( (line=reader.ReadLine()) != null)
{
if (line.StartsWith ("<START_OF_SECTION:1>"))
writing=true;
if (writing)
{
Response.Write (line);
Response.Write ("<br/>");
}
if (line.StartsWith ("<END_OF_SECTION:1>"))
writing=false;
}
}
Response.Write ("</body>");
Response.Write ("</html>");
Note that the above means you get the lines with both tags in - your
own code would only include the start tag. It's only got the call to
ReadLine once, which helps (IMO) at the slight cost of having a minorly
complicated idiom (which is very commonly used though - you get used to
it very quickly) for the while condition.
Having the boolean variable with a meaningful name just helps
readability in general.
The using(...) construct means that your reader gets closed even if an
exception occurs.
You might also consider explicitly stating what encoding you expect the
file to be in, when you construct the StreamReader. If it's UTF-8 then
it won't change behaviour, but it'll make it more obvious when reading
the code.
--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too