File.OpenRead

G

Guest

I read a txt file with html. The openread event removes all the html tags.
Any ideas...?


public static string fileOpen(string fileName)
{
string mRet = string.Empty;

// Open the stream and read it back.
using (FileStream fs = File.OpenRead(fileName))
{
byte[] b = new byte[1024];
UTF8Encoding temp = new UTF8Encoding(true);

while (fs.Read(b, 0, b.Length) > 0)
{
//Trace.WriteLine(temp.GetString(b));
mRet = temp.GetString(b);
}
}

return mRet;
}

thanks
 
T

Tom Spink

mgonzales3 said:
I read a txt file with html. The openread event removes all the html
tags. Any ideas...?


public static string fileOpen(string fileName)
{
string mRet = string.Empty;

// Open the stream and read it back.
using (FileStream fs = File.OpenRead(fileName))
{
byte[] b = new byte[1024];
UTF8Encoding temp = new UTF8Encoding(true);

while (fs.Read(b, 0, b.Length) > 0)
{
//Trace.WriteLine(temp.GetString(b));
mRet = temp.GetString(b);
}
}

return mRet;
}

thanks

Hi,

Check your code, and your input; make sure you're reading the file that you
think you're reading... Removing HTML tags is a very specific thing to be
happening, and I highly doubt it's a malfunction of OpenRead.
 
G

Guest

Hi

I just ran through your code, i did not see any issues with the OpenRead
statement. However, there is one thing to note here, the mRet variable is
everytime overwritten in the loop, where i think a possible concatenation has
to happen, as because if the file you read has more than 1024 bytes, then you
will have only the last read 1024 bytes in the return. Check, if that is
creating the problem

Prem
 
M

Marc Gravell

Like the other respondant, I'm not 100% sure of your symptoms (can you
reproduce this with postable html?) - however - some thoughts:

0: doing too much! In 2.0 can simply use:

return File.ReadAllText(fileName, new UTF8Encoding(true));

1: concatenation (like already posted) - although for large files you
might want something more efficient like StringBuilder (below)

2: not using Stream.Read return value in the call to Encoding.GetString
(although I suspect that FileStream is forgiving and fills the buffer
when possible) - but this does mean you will be over-running the last
block - i.e.

StringBuilder sb = new StringBuilder();
const int BUFFER_SIZE = 1024;
byte[] b = new byte[BUFFER_SIZE];
UTF8Encoding temp = new UTF8Encoding(true);
int bytesRead;
while ((bytesRead = fs.Read(b, 0, BUFFER_SIZE)) > 0) {
sb.Append(temp.GetString(b, 0, bytesRead);
}
return sb.ToString();

3: encoding; are you sure it is UTF8?

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top