format of txt file lost

D

doomsday123

I have a txt file that I am reading and displaying on a page. The text
in the txt is...
------------------------------------------------------------------------------------------
Discontinued P/N Replacement P/N
71-TNPW0603100KBEBD 71-TNPW0603100KBETY
71-TNPW0603100RBEBD 71-TNPW0603100RBETY
71-TNPW0603105KBEBD 71-TNPW0603105KBETY
------------------------------------------------------------------------------------------



When I use this method to display the text on a page...
------------------------------------------------------------------------------------------
StreamReader fsText = fi.OpenText();

StringBuilder sb = new StringBuilder();

while (!fsText.EndOfStream)
{
sb.Append(fsText.ReadLine() + "<br />");
}
------------------------------------------------------------------------------------------



The text comes out like this...
------------------------------------------------------------------------------------------
Discontinued P/N Replacement P/N
71-TNPW0603100KBEBD 71-TNPW0603100KBETY
71-TNPW0603100RBEBD 71-TNPW0603100RBETY
71-TNPW0603105KBEBD 71-TNPW0603105KBETY
------------------------------------------------------------------------------------------


Is there any way to get the text to display exactly like it is in the
txt file?
Thanks
 
M

mpetrotta

I have a txt file that I am reading and displaying on a page. The text
in the txt is...
------------------------------------------------------------------------------------------
Discontinued P/N Replacement P/N
71-TNPW0603100KBEBD 71-TNPW0603100KBETY
71-TNPW0603100RBEBD 71-TNPW0603100RBETY
71-TNPW0603105KBEBD 71-TNPW0603105KBETY
------------------------------------------------------------------------------------------

When I use this method to display the text on a page...
------------------------------------------------------------------------------------------
StreamReader fsText = fi.OpenText();

StringBuilder sb = new StringBuilder();

while (!fsText.EndOfStream)
{
sb.Append(fsText.ReadLine() + "<br />");
}
------------------------------------------------------------------------------------------

The text comes out like this...
------------------------------------------------------------------------------------------
Discontinued P/N Replacement P/N
71-TNPW0603100KBEBD 71-TNPW0603100KBETY
71-TNPW0603100RBEBD 71-TNPW0603100RBETY
71-TNPW0603105KBEBD 71-TNPW0603105KBETY
------------------------------------------------------------------------------------------

Is there any way to get the text to display exactly like it is in the
txt file?
Thanks

It looks like you're creating an HTML file. If you open the HTML file
in a text editor, you'll see that the spaces are there as expected.
The culprit here is your browser, collapsing multiple spaces to a
single space. See http://www.sightspecific.com/~mosh/WWW_FAQ/nbsp.html
for more information.

What can you do? You can substitute, as the page above discusses, a
&nbsp for a space. Or you can substitute it only when you see
multiple spaces in sequence. Or you can convert the formatted table
to a proper HTML table, if possible.

This is an HTML issue, not a C# one, so you may get more help in an
HTML newsgroup.

Michael
 
N

Nicholas Paldino [.NET/C# MVP]

Using &nbsp to replace the spaces is indeed the correct way to go. In
order to do it on the server side, you can call the HtmlEncode method on the
HttpServerUtility instance exposed by the page you are on (through the
Server property). This will take a string and return an HTML-encoded
string, or you can write it to a TextWriter if you want.

It should be noted that this isn't a "problem" with the browser, but
rather, the way HTML is supposed to behave, this is ^expected^ behavior.

On the browser side, you are most likely going to want to use a
fixed-width font for the presentation of this data, as spaces in
non-fixed-width fonts usually tend to be small, and you won't have that much
space between them.

Either that, or as Michael says, use tables to perform the layout (which
would require more parsing on your end).
 

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