解析为纯文本新段

时间:2009-06-24 23:01:41

标签: c# html string

我目前正在从内部数据库中的字段中删除html标记。除了将
标签转换为纯文本新行字符外,一切都顺利进行。

我想转换一下:

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.

到此:

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.

我使用以下代码行将
更改为新行字符:

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

运行该代码后,这就是我的数据库中保存的内容:

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.

如果我将解析后的文本保存到我的数据库并将其粘贴到记事本或Word中,我只能获得一个而不是两个。

这是处理此问题的正确方法吗?我使用的数据库是SQL Server 2005。

4 个答案:

答案 0 :(得分:4)

您使用 Environment.Newline 的方法是正确的。我认为问题在于如何直接在SQL Server中返回一些查询,假设您直接从SQL Server Management Studio(或类似)复制/粘贴。

如果您使用SqlConnection将数据拉出并将其输出到winform,文本文件等,那么我肯定会有99%的肯定...那么您将获得您正在寻找的换行符。< / p>

很抱歉,但我不记得为什么当您直接从SQL Server中的结果网格中复制/粘贴时会发生这种情况。

答案 1 :(得分:2)

根据您的后续评论(当您调试它时),听起来正确的值至少正确地发送到数据库。

这可能不是这么简单,但值得检查......当你说“运行查询以获取值...并将其粘贴到Word”时,你用什么来进行查询?因为我知道如果您在默认的“结果到网格”视图中使用SQL Server 2005 Management Studio查询某些内容,它不会正确呈现新行(我认为它只是用空格替换它们)...如果您将其切换为“结果到文本“(或者您从代码中获取数据库中的值并调试返回的值),您将获得更准确的实际值表示,并显示新行...

答案 2 :(得分:1)

我很好奇你是如何检索“已保存”的值。您是从SQL Server Management Studio复制它,还是实际执行SELECT语句?有时,在SQL Server 2005工具中显示信息的数据网格不会“精确”显示字符串数据,因为它存储在数据库中。如果您还没有实际执行SELECT语句,我会尝试,并确保您没有遇到UI怪癖。

答案 3 :(得分:0)

您是否尝试使用实际的换行符替换?即。

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

授予环境.NewLine 应该做同样的事情,但值得一试。