简体   繁体   English

解析 <br> 明文新段落

[英]Parse <br> to plain text new paragraph

I am currently in the process of removing html tags from fields within an internal database. 我目前正在从内部数据库中的字段中删除html标记。 Everything has gone smoothly except for turning 除了转弯之外,一切都很顺利
tags to plain text new line characters. 标签为纯文本换行符。

I would like to convert this: 我想转换这个:

The victory halted Spain&rsquo;s 35-game unbeaten streak, handing the Spanish their first defeat since November 2006. The Americans now advance to the final Sunday to face the winner of Thursday&rsquo;s semifinal between South Africa and Brazil, the five-time World Cup winner. Brazil defeated the Americans, 3-0, in their earlier meeting in this tournament.<br>
<br>
In the final, though, the United States will be without midfielder Michael Bradley, who received a red card for a harsh tackle in the 87th minute, the third such ejection for the Americans in this tournament. It was the only careless blemish on an otherwise nearly perfect evening.

in to this: 对此:

The victory halted Spain’s 35-game unbeaten streak, handing the Spanish their first defeat since November 2006. The Americans now advance to the final Sunday to face the winner of Thursday’s semifinal between South Africa and Brazil, the five-time World Cup winner. Brazil defeated the Americans, 3-0, in their earlier meeting in this tournament.

In the final, though, the United States will be without midfielder Michael Bradley, who received a red card for a harsh tackle in the 87th minute, the third such ejection for the Americans in this tournament. It was the only careless blemish on an otherwise nearly perfect evening.

I am using the following line of code to change the 我使用以下代码行来更改
to a new line character: 到一个新行字符:

value = value.Replace("<br>", Environment.NewLine).Trim();

After running that code this is what is saved in my database: 运行该代码后,这是我的数据库中保存的内容:

The victory halted Spain's 35-game unbeaten streak, handing the Spanish their first defeat since November 2006. The Americans now advance to the final Sunday to face the winner of Thursday's semifinal between South Africa and Brazil, the five-time World Cup winner. Brazil defeated the Americans, 3-0, in their earlier meeting in this tournament.    In the final, though, the United States will be without midfielder Michael Bradley, who received a red card for a harsh tackle in the 87th minute, the third such ejection for the Americans in this tournament. It was the only careless blemish on an otherwise nearly perfect evening.

If I take the parsed text saved to my database and paste it into notepad or Word I get only one paragraph instead of two. 如果我将解析后的文本保存到我的数据库并将其粘贴到记事本或Word中,我只得到一个而不是两个。

Is this the correct way to handle this? 这是处理这个问题的正确方法吗? The database I am using is SQL Server 2005. 我使用的数据库是SQL Server 2005。

Your method of using Environment.Newline is correct. 您使用Environment.Newline的方法是正确的。 I believe the issue is with how some queries are returned directly in SQL Server, assuming you're copy/pasting directly out of SQL Server Management Studio (or similar). 我认为问题在于如何直接在SQL Server中返回一些查询,假设您直接从SQL Server Management Studio(或类似)复制/粘贴。

I'm about 99% positive that if you pull the data out with a SqlConnection and then output it to a winform, text file, etc... then you'll get the line breaks you're looking for. 我大约99%肯定,如果你用SqlConnection拉出数据,然后将它输出到winform,文本文件等...那么你将得到你正在寻找的换行符。

Sorry, but I can't recall why this happens when you copy/paste directly out of the grid of results in SQL Server. 抱歉,但我不记得为什么当您直接从SQL Server中的结果网格中复制/粘贴时会发生这种情况。

Based on your follow up comment (when you debugged it), it sounds like the correct value is at least being sent to the database correctly. 根据您的后续注释(当您调试它时),听起来正确的值至少是正确发送到数据库。

It's probably not this simple, but worth checking... When you say you "ran a query to pull the value ... and pasted it to Word", what are you using to do the query? 它可能不是这么简单,但值得检查...当你说“运行查询以获取值...并将其粘贴到Word”时,你用什么来进行查询? Because I know if you query something using SQL Server 2005 Management Studio in the default "Results to Grid" view, it doesn't render new lines properly (I think it just replaces them with spaces)... If you switch it to "Results to Text" (or you get the value from the database in your code & debug the value returned), you'll get a more accurate representation of the actual value, complete with new lines showing... 因为我知道如果您在默认的“结果到网格”视图中使用SQL Server 2005 Management Studio查询某些内容,它不会正确呈现新行(我认为它只是用空格替换它们)...如果您将其切换为“结果到文本“(或者您从代码中获取数据库中的值并调试返回的值),您将获得更准确的实际值表示,并显示新行显示...

I am curious how you are retrieving the "saved" value. 我很好奇你是如何检索“已保存”的值。 Are you copying it from, say, SQL Server Management Studio, or actually performing a SELECT statement? 您是从SQL Server Management Studio复制它,还是实际执行SELECT语句? Sometimes, the data grids that display information in the SQL Server 2005 tools don't display string data "exactly" as it is stored in the database. 有时,在SQL Server 2005工具中显示信息的数据网格不会“精确”显示字符串数据,因为它存储在数据库中。 If you have not actually performed a SELECT statement, I would try that, and make sure you are not encountering a UI quirk. 如果您还没有实际执行SELECT语句,我会尝试,并确保您没有遇到UI怪癖。

Have you tried replacing with actual newline characters? 您是否尝试使用实际的换行符替换? ie

value = value.Replace("<br>", "\r\n").Trim();

Granted Environment.NewLine should do this same thing but it's worth a shot. 授予环境.NewLine 应该做同样的事情,但值得一试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM