How can I validate text as being valid HTML?

老子叫甜甜 提交于 2019-12-09 19:12:15

问题


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!


回答1:


You could start here:

C# XHTML Validator

Or here (regexes):

Regular Expressions Library - entries tagged with "HTML"

Or here ;)

Google




回答2:


regexes can be slow and cpu intensive, W3C has an open source tool that you could use http://www.w3.org/QA/Tools/




回答3:


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




回答4:


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 property Xhtml that ensures the you get compliant, XML based content (there were also properties like SaveAsXhtml 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 the Html property is deprecated in favour of the Content property, so I assume this is outputting valid Xhtml by default.



来源:https://stackoverflow.com/questions/1435650/how-can-i-validate-text-as-being-valid-html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!