html中的字符串输出不正确

发布于 2025-01-04 05:41:01 字数 2762 浏览 1 评论 0原文

我有一个问题,我正在尝试以 html 格式输出文本文件文本,但是我希望 html 输出中的格式与文本文件格式相同。因此,在我的文本文件中,我有这样的:

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.

注意它在我的文本文件中如何使用空格和新段落进行格式化。这是我的代码..

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();
        }
    }
}

前端代码:

<%@ 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>

当我运行它时,我得到了错误的格式..哪个是我的问题..输出如下..

Hello Test, Settings:[用户名] //您的用户名是什么Setting = Value //要设置的设置Testing this:Esse meis has in, in per suavitate 发生。 Eu quo homero fabulas democritum。斜交双鱼 ad,unum saepe eirmod te sed,ius vidit vidisse cu。一切皆有可能 viris ius.Nam fairi dere ei。 Eos aliquip mentitum persequeris 号。 Nec gubergren contentiones cu, ea quoautem nostrud。初始值 probatus, tempor vocent pertinacia ex est. Volumus oportere usu cu,clita facete Perfecto ea has,nam eu unumeverti inimicus。

正如你所看到的,它的格式是错误的,任何人都可以帮助我多年来一直试图解决这个问题。

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..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

撩起发的微风 2025-01-11 05:41:01

尝试将内容放入

tag
 中。

这会通知浏览器内容已预先格式化并遵循新行。

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

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

美人骨 2025-01-11 05:41:01

您需要查找并替换换行符和回车符:

Regex regex = new Regex(@"(\r\n|\r|\n)+");
string data = regex.Replace(fileData, "<br />");

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 />");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文