把文本保存到数据库时如何保存文本的格式?

dlss 2002-11-14 04:21:32
把一段文本保存到数据库后,再用Web页面显示时,发现文本都串在一起了,段落等格式没有了,如何解决?
...全文
118 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
dlss 2002-11-15
  • 打赏
  • 举报
回复
我找到了,给大家共享
public string Parser(string text, bool allow)
{
//Create a StringBuilder object from the string intput parameter
StringBuilder sb = new StringBuilder(text) ;
//Replace all double white spaces with a single white space and  
sb.Replace(" "," ");
//Check if HTML tags are not allowed
if(!allow)
{
//Convert the brackets into HTML equivalents
sb.Replace("<","<") ;
sb.Replace(">",">") ;
//Convert the double quote
sb.Replace("\"",""");
}
//Create a StringReader from the processed string of the StringBuilder object
StringReader sr = new StringReader(sb.ToString());
StringWriter sw = new StringWriter();
//Loop while next character exists
while(sr.Peek()>-1)
{
//Read a line from the string and store it to a temp variable
string temp = sr.ReadLine();
//write the string with the HTML break tag
//Note here write method writes to a Internal StringBuilder object created automatically
sw.Write(temp+"<br/>") ;
}
//Return the final processed text
return sw.GetStringBuilder().ToString();
}
lotustao 2002-11-14
  • 打赏
  • 举报
回复
保存文本时应该保存innerHtml

62,057

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧