0
votes

I have an application where the user can write articles and they will be displayed on a web page. Instead of storing all the text in the database I want to write it to a text file and store it that way. The problem is, I can't seem to get the file to hold formatting ie line breaks. If the user types

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed eget ligula eget quam consectetur ornare. Integer sed sapien risus, ac tincidunt lectus. Quisque ultrices mi tortor. Fusce luctus magna non dolor facilisis imperdiet. Praesent tempus gravida dolor, sit amet viverra ligula vehicula sit amet.

Donec congue sodales orci, in sodales nunc fermentum quis. Proin feugiat feugiat ultrices. Mauris iaculis libero non nibh sagittis fermentum. Etiam odio sapien,

pulvinar sed viverra nec, euismod eu massa. Sed pulvinar diam a velit rhoncus non iaculis mi facilisis.

and then saves, it is saved as

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed eget ligula eget quam consectetur ornare. Integer sed sapien risus, ac tincidunt lectus. Quisque ultrices mi tortor. Fusce luctus magna non dolor facilisis imperdiet. Praesent tempus gravida dolor, sit amet viverra ligula vehicula sit amet. Donec congue sodales orci, in sodales nunc fermentum quis. Proin feugiat feugiat ultrices. Mauris iaculis libero non nibh sagittis fermentum.Etiam odio sapien, pulvinar sed viverra nec, euismod eu massa. Sed pulvinar diam a velit rhoncus non iaculis mi facilisis.

Is there any way to maintain formatting?

Current code is

var fileContents = System.IO.File.ReadAllText(Server.MapPath(@"~/Articles/" + articlePost.atclFile + ".txt"));
articleTextbox.Text = fileContents.Replace(System.Environment.NewLine, "<br/>");
3
@AustinSalonen eddited to show codeWesley Skeen
What is articleTextbox? A textbox?System Down
Hi it was a literal. I got the solution thought. Mikes solution worked. Thanks for your inputWesley Skeen

3 Answers

2
votes

What about

articleTextbox.Text = fileContents.Replace("\r\n", "<br/>");

or

articleTextbox.Text = fileContents.Replace("\n", "<br/>");
4
votes

You need to replace text line breaks with HTML line breaks:

var newText = oldText.Replace(System.Environment.NewLine, "<br/>");
1
votes

how about the good old

     <pre> ... </pre>