我有一个问题我试图在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();
}
}
}
FRONT END代码:
<%@ 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,设置:[用户名] //您的usernameSetting = Value是什么 //要设置的设置测试:Esse meis 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。
正如你所看到的那样格式错误任何人都可以帮助我多年来一直试图解决这个问题..
答案 0 :(得分:4)
尝试将内容放在<pre>tag<pre>
。
这会通知浏览器内容已预先格式化并尊重新行。
答案 1 :(得分:0)
您需要查找并替换换行符和回车符:
Regex regex = new Regex(@"(\r\n|\r|\n)+");
string data = regex.Replace(fileData, "<br />");