如何验证文本是否为有效的 HTML?
我使用的是旧版本的 Telerik Editor 控件,并将输入的文本存储在 SQL 数据库中,然后显示 HTML。 ASP.NET (2.0) 中有什么方法可以在保存到数据库之前验证字符串是否为有效的 HTML?谢谢!
I'm using an older version of Telerik's Editor control, and storing the text that's input in a SQL database, then displaying the HTML later. Is there any way in ASP.NET (2.0) to validate a string as being valid HTML before saving to my database? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以从这里开始:
C# XHTML Validator
或者在这里(正则表达式):
正则表达式库 - 标记为“HTML”的条目
或者在这里 ;)
谷歌
You could start here:
C# XHTML Validator
Or here (regexes):
Regular Expressions Library - entries tagged with "HTML"
Or here ;)
Google
正则表达式可能很慢并且占用 CPU 资源,W3C 有一个开源工具,您可以使用 http://www .w3.org/QA/Tools/
regexes can be slow and cpu intensive, W3C has an open source tool that you could use http://www.w3.org/QA/Tools/
该编辑器有一个内置的验证器对话框,可以连接到在线 W3 工具来检查当前内容。您可以在此处查看示例 - XHTML 验证器示例
The editor has a built-in validator dialog that connects to the online W3 tool to check the current content. You can see an example here - XHTML Validator example
嗯 - 您使用的是哪个版本(“旧版本”不是很具体)?
在 .Net 1.1/2.0 版本中(我认为是 2004/2005 年的版本 3.x/4.x),编辑器 API 包含一个读/写属性
Html
,允许您设置内容作为 HTML,还可以检索它以存储在任何位置,以及只读属性Xhtml
确保您获得兼容的、基于 XML 的内容(还有像这样的属性>SaveAsXhtml
以确保如果您写回文件,它将采用 XHTML 格式)。我明白,如果您的页面向 HTML 4.0 提供内容,那么您可能会遇到一些封闭标记的问题(
、![]()
、 ETC)。请记住,有效的 HTML 和干净的 HTML 之间是有区别的。
我看到 当前版本 不再具有
Xhtml
属性,并且Html
属性已被弃用,取而代之的是Content
属性,所以我假设这个默认情况下输出有效的 Xhtml。Hmm - which version are you using ("older version" isn't terribly specific)?
In the .Net 1.1/2.0 versions (I think about version 3.x/4.x from 2004/2005) the editor API consisted of a read/write property
Html
that allowed you to set the content as HTML, and also retrieve it for storing where ever, and a read only propertyXhtml
that ensures the you get compliant, XML based content (there were also properties likeSaveAsXhtml
to ensure that if you were writing back to the file, it would be in XHTML).I appreciate that if your page is serving content to HTML 4.0 then you might have some issues with closed tags (
<br />
,<img />
, etc).Remember there's a difference between valid HTML and clean HTML.
I see that the current version no longer has an
Xhtml
property, and that theHtml
property is deprecated in favour of theContent
property, so I assume this is outputting valid Xhtml by default.