检测只包含HTML和无文本字符串 [英] Detecting string containing only HTML and no text
本文介绍了检测只包含HTML和无文本字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在C#中可以检查一个HTML字符串是否确实包含一些文本或只是仅由HTML标签和实体的?
例如
字符串str = @< p =的xmlnshttp://www.w3.org/1999/xhtml/> ;
这仅仅包含HTML标签,并没有文字。
解决方案
的XDocument DOC = XDocument.Parse(yourString);
布尔containsText = doc.Root.DescendantNodes()
.Count之间(EL = GT; el.GetType()== typeof运算(XTEXT))> 0
提示:
我经常结合了 SGMLReader 这种方法来确保XDocument.Parse有效的XML (...)
Is it possible in C# to check whether a HTML string actually contains some text or is just made up of HTML tags and entities only?
For example
string str = @"<p xmlns=""http://www.w3.org/1999/xhtml"" />"
This contains only HTML tag and no text.
解决方案
XDocument doc = XDocument.Parse(yourString);
bool containsText = doc.Root.DescendantNodes()
.Count(el => el.GetType() == typeof (XText)) > 0
Tip:
I often combine this approach with SGMLReader to ensure valid xml for XDocument.Parse(...)
这篇关于检测只包含HTML和无文本字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文