File IO Read

G

Guest

I am having a an issue trying to read a text file using the File.Read command.
Here's the code:

FileStream fs = File.Open ("somefile.xml",Open,Read,ReadWrite);
StringBuilder strXml = new StringBuilder();
byte[] b = new byte[1024];
UTF8Encoding temp = new UTF8Encoding(true);

while (fs.Read(b,0,b.Length) > 0)
{
strXml.Append(temp.GetString(b));
}
fs.Close();


The problem I'm having is when I look at the finished string
strXml.toString() it contains the cr / lf characters(\r\n) ie:

<COOKIES>\r\n <IDGROUP>somegroup</IDGROUP>\r\n

I do not want these non-printable characters, only the visual text.
I was hoping there is a setting I am missing.
Any help is greatly appreciated.

thanks
paul
 
P

Peter Rilling

Does this only happen when you are viewing the item in the debugger? These
character always appear in the debugger, but not if you were to write them
out to a file.
 
G

Guest

Try this:

TextReader reader = File.OpenText("somefile.xml");
string strXml = reader.ReadToEnd();
reader.Close();

If you want to remove all the \r and \n from the string you could do
something like:
strXml = System.Text.RegularExpressions.Regex.Replace(strXml, @"[\r\n]", "");

Hope that helps.
 

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