What the program does is goes out to a webpage, reads the source line by line, strips out the html tags/code, and then writes the actual text/information to a text file. Because I want the text file to only contain the data/information I want and nothing else, I have it looking for a specific string that tells it it's found the right section so it can start writing to the text file, and then it looks for another string to know when it's found the end of the section. It correctly starts and stops at the right lines in the file (I put in a counter to make sure, and it was correctly stopping/starting in the right spots), however, it doesn't finish writing all of the information to text file. It doesn't even stop at the end of a specific line -- it often stops writing in the middle of a line. As an example, if I want it to read 4 lines, the output might look like:
Text for line 1.. Text for line 2.. Text for li
and just stop, even though it should read/write 4 lines.
Below is my code:
private void button1_Click(object sender, EventArgs e)
{
string tempString = "";
string startString = "start string";
string endString = "end string";
bool startFlag = false;
bool endFlag = true;
string filename = @"C:\file.txt";
System.IO.StreamWriter file = new System.IO.StreamWriter(filename);
WebRequest request = WebRequest.Create("http://www.website.com/webpage.html");
request.Credentials = CredentialCache.DefaultCredentials;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Console.WriteLine(response.StatusDescription);
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
while (endFlag)
{
tempString = reader.ReadLine();
if (tempString.Contains(startString))
{
startFlag = true;
}
if (tempString.Contains("text"))
{
...
}
if (tempString.Contains("other text"))
{
if (startFlag)
file.WriteLine(tempString.Trim());
}
if (tempString.Contains("different text"))
{
if (startFlag && tempString.Length > 0)
file.WriteLine(tempString.Trim());
}
if (tempString.Contains(endString))
{
endFlag = false;
}
}
MessageBox.Show("Done!", "Finished Writing", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
reader.Close();
dataStream.Close();
response.Close();
Process.Start(filename);
this.Close();
}
I've tried just about everything I can think of, I've tried using flush, I've tried having it continue reading/writing past the string that signals the end of the section, but nothing seems to work. I know I'm not the greatest coder by any means, but I'm really stumped. Can anyone share some insight? Any help greatly appreciated!