No lines with text file with asp.net

I want to read a text file and load its contents into my page. I tried to read a file with StreamReader and then assigned text to a shortcut, but the text on the page is just one line. I mean the line in the text file was not visible on the page. What should I do?

+2


source to share


5 answers


Perhaps solving the problem, rather than the problem itself, you can wrap the content of the text file in a tag <PRE>

that, unlike most other content in HTML, respects free space.



+2


source


The text file uses \ n or \ r \ n to get new lines (\ n is a newline character, and \ r is a carriage return on the day of the typescript entries you had to pull out in the panel from left to left, called carriage return and roll paper down the line to start on the left side of a new line -). Windows usually uses \ r \ n (although it depends on the application that created the file) mac usually uses \ n.

HTML on the other hand uses the <br/

> tag for new lines (if you use the viewource on your current html output you will see new lines). So all you have to do is replace \ r \ n or
. You can do it with:

yourstring = yourstring.Replace("\r\n", "<br/>");

      



or if you don't know exactly what is used in the file, or both \ r \ n and \ n are used, you can use

yourstring = yourstring.Replace("\r\n", "<br/>").Replace("\n", "<br/>");

      

remember that the string is immutable and thus methods such as Replace return a copy of the string that has the replacements made. The original string will remain unchanged.

+1


source


Please, try

if (System.IO.File.Exists (Server.MapPath ("test.txt")))

    {
        System.IO.StreamReader StreamReader1 = new 

      

System.IO.StreamReader (Server.MapPath ("test.txt"));

        lblMyLabel.Text= StreamReader1.ReadToEnd();

        StreamReader1.Close();
    }

      

+1


source


HTML doesn't recognize whitespace (line breaks, etc.) in a text file. If you want to display content as HTML, you need to convert line breaks to <br/

> tags .

0


source


Try something like this:

string path = 'c:\myfile.txt':
lblMyLabel.Text = String.Join('<br/>', File.ReadAllLines(path));

      

0


source







All Articles