I have an issue i'm trying to output textfile text in html however I want the same format in the html output to match the textfile format. So example in my textfile i have this:

Hello Test, Settings:    
Setting = Value //The setting to be set

Testing this:

Esse meis has in, in per suavitate ocurreret. Eu quo homero fabulas democritum. Dico oblique
veritus quo ad, unum saepe eirmod te sed, ius vidit vidisse cu. Eu quis omnis viris ius.

Nam falli decore ei. Eos aliquip mentitum persequeris no. Nec gubergren contentiones cu, ea quo
autem nostrud. Mea ex primis probatus, tempor vocent pertinacia ex est. Volumus oportere usu cu,
clita facete perfecto ea has, nam eu unum everti inimicus.

NOTICE how its formatted with spaces and new paragraph in my textfile. Here is my code..

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.IO;

namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            String line; StringBuilder sp = new StringBuilder();
            using (StreamReader sr = new StreamReader("TextFile1.txt")) //PATH WHERE YOU HAVE STORED THE TEXT FILE
            {

                // Read and display lines from the file until the end of
                // the file is reached.
                while ((line = sr.ReadLine()) != null)
                {
                    sp.Append(line);
                }

            }
            dvText.InnerText = sp.ToString();
        }
    }
}

FRONT END code:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1"
    ValidateRequest="false" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div id="dvText" runat="server">

    </div>
    </form>
</body>
</html>

When I run it i get wrong format.. which is my problem.. output below..

Hello Test, Settings:[Username] //What is your usernameSetting = Value //The setting to be setTesting this:Esse meis has in, in per suavitate ocurreret. Eu quo homero fabulas democritum. Dico obliqueveritus quo ad, unum saepe eirmod te sed, ius vidit vidisse cu. Eu quis omnis viris ius.Nam falli decore ei. Eos aliquip mentitum persequeris no. Nec gubergren contentiones cu, ea quoautem nostrud. Mea ex primis probatus, tempor vocent pertinacia ex est. Volumus oportere usu cu,clita facete perfecto ea has, nam eu unum everti inimicus.

As you can see it formats it wrong can anyone help me been trying to solve this issue for ages..

有帮助吗?

解决方案

Try to put the content inside a <pre>tag<pre>.

This notifies the browser that the content is preformatted and respects the new lines.

其他提示

You need to find and replace line feeds and carriage returns:

Regex regex = new Regex(@"(\r\n|\r|\n)+");
string data = regex.Replace(fileData, "<br />");
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top