如何让这个 HTML 显示格式,没有标签?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7942133/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get this HTML to display formatted, without tags?
提问by nanonerd
I created a blog (for coding practice). I use a Rich Text Editor (ckeditor) and save the post to a database. When I pull the post out to display in a Label.Text, it shows all the HTML tags:
我创建了一个博客(用于编码练习)。我使用富文本编辑器(ckeditor)并将帖子保存到数据库中。当我将帖子拉出以显示在 Label.Text 中时,它会显示所有 HTML 标签:
<p><strong>there was</strong> once a fox that lived</p>
<p> in the<span style="color: #ff0000"> woods</span></p>
How do I get the post to display, with the proper formatting (paragraph, color, etc.), but without the HTML tags?
如何使用正确的格式(段落、颜色等)显示帖子,但没有 HTML 标签?
回答by nanonerd
FYI: The literal produced the same result as the Label ... but I got my answer, this works:
仅供参考:文字产生的结果与标签相同......但我得到了答案,这有效:
string strHTML = "<p>Hello World!</p>";
Label.Text = Server.HtmlDecode(strHTML);
回答by ChrisF
Well the HTML isthe formatting, so to get the text to display how you want you need to insert the text as HTMLinto your page rather than inserting the text into the Label.Text - which will treat is as plain text and display all the markup.
嗯,HTML是格式,所以要让文本显示您希望如何将文本作为 HTML插入到您的页面中,而不是将文本插入到 Label.Text 中 - 它将被视为纯文本并显示所有标记。
So rather than create a Label use a Literal:
因此,与其创建标签,不如使用文字:
<asp:Literal runat="server" ID="EditorOutput">
Then in your page load:
然后在您的页面加载中:
protected void Page_Load(object sender, EventArgs e)
{
EditorOutput.Text = theText;
}
theText
will be the string:
theText
将是字符串:
<p><strong>there was</strong> once a fox that lived</p> <p> in the<span style="color: #ff0000"> woods</span></p>
as read from your database.
从您的数据库中读取。
If your string has been Encodedyou will have to call Server.HtmlDecode
on it to make sure that any <
and >
codes are converted back to <
and >
.
如果您的字符串已被编码,您将不得不调用Server.HtmlDecode
它以确保 any<
和>
代码被转换回<
and >
。
回答by nanonerd
I didn't understand. Do you mean, when you check the post, you get something like:
我不明白。你的意思是,当你查看帖子时,你会得到类似的信息:
<b>there was</b> once a fox that lived...
It's probably a good idea to save it in .html format, since RTFs were never meant for the internet. It also sounds like you don't need to use a database in the first place. XML is better for that kind of task.
将其保存为 .html 格式可能是个好主意,因为 RTF 从来就不是用于 Internet 的。听起来您一开始就不需要使用数据库。XML 更适合这种任务。