File IO Read

  • Thread starter Thread starter Guest
  • Start date Start date
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
 
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.
 
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.
 
Back
Top