473,395 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

File IO Read

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

Jul 21 '05 #1
2 1382
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.

"paul" <pa**@discussions.microsoft.com> wrote in message
news:99**********************************@microsof t.com...
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

Jul 21 '05 #2
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(strXm l, @"[\r\n]", "");

Hope that helps.

--
Jason Whitted

"paul" wrote:
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

Jul 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: simon place | last post by:
is the code below meant to produce rubbish?, i had expected an exception. f=file('readme.txt','w') f.write(' ') f.read() ( PythonWin 2.3 (#46, Jul 29 2003, 18:54:32) on win32. ) I got...
6
by: Russell E. Owen | last post by:
At one time, mixing for x in file and readline was dangerous. For example: for line in file: # read some lines from a file, then break nextline = readline() # bad would not do what a naive...
3
by: Abhas | last post by:
> > Hi, this is Abhas, > > I had made a video library program in C++, but was facing a problem. > > After entering 12 movies, i cannot enter any more movies. > > Something gibberish comes instead....
8
by: siliconwafer | last post by:
Hi All, If I open a binary file in text mode and use text functions to read it then will I be reading numbers as characters or actual values? What if I open a text file and read it using binary...
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
13
by: George | last post by:
Hi, I am re-writing part of my application using C#. This application starts another process which execute a "legacy" program. This legacy program writes to a log file and before it ends, it...
9
by: Use*n*x | last post by:
Hello, I have a binary file (image file) and am reading 4-bytes at a time. The File size is 63,480,320 bytes. My assumption is that if I loop through this file reading 4 bytes at a time, I...
3
by: JDeats | last post by:
I have some .NET 1.1 code that utilizes this technique for encrypting and decrypting a file. http://support.microsoft.com/kb/307010 In .NET 2.0 this approach is not fully supported (a .NET 2.0...
1
AdrianH
by: AdrianH | last post by:
Assumptions I am assuming that you know or are capable of looking up the functions I am to describe here and have some remedial understanding of C programming. FYI Although I have called this...
15
by: patf | last post by:
Hi - experienced programmer but this is my first Python program. This URL will retrieve an excel spreadsheet containing (that day's) msci stock index returns. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.