如何验证,使用C#中的字符串不包含HTML [英] How to validate that a string doesn't contain HTML using C#

查看:315
本文介绍了如何验证,使用C#中的字符串不包含HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

人是否有检查一个字符串不包含HTML的一种简单,有效的方法?基本上,我想检查,某些领域的只包含纯文本。我想过找的<性格,但可以很容易地以纯文本使用。另一种方式可以是使用以创建新System.Xml.Linq.XElement:

Does anyone have a simple, efficient way of checking that a string doesn't contain HTML? Basically, I want to check that certain fields only contain plain text. I thought about looking for the < character, but that can easily be used in plain text. Another way might be to create a new System.Xml.Linq.XElement using:

XElement.Parse("<wrapper>" + MyString + "</wrapper>")

和检查的XElement不包含子元素,但这似乎什么,我需要一点点重量级的。

and check that the XElement contains no child elements, but this seems a little heavyweight for what I need.

推荐答案

我只是想我的XElement.Parse解决方案。我创建了String类的扩展方法,所以我可以重复使用code容易:

I just tried my XElement.Parse solution. I created an extension method on the string class so I can reuse the code easily:

public static bool ContainsXHTML(this string input)
{
    try
    {
        XElement x = XElement.Parse("<wrapper>" + input + "</wrapper>");
        return !(x.DescendantNodes().Count() == 1 && x.DescendantNodes().First().NodeType == XmlNodeType.Text);
    }
    catch (XmlException ex)
    {
        return true;
    }
}

一个问题我发现的是,纯文本的符号和小于字符导致XmlException并表明该字段包含HTML(这是错误的)。为了解决这个问题,在首先需要通过输入字符串具有与号和小于转换为等效的XHTML实体的字符。我写了另一个扩展方法来做到这一点:

One problem I found was that plain text ampersand and less than characters cause an XmlException and indicate that the field contains HTML (which is wrong). To fix this, the input string passed in first needs to have the ampersands and less than characters converted to their equivalent XHTML entities. I wrote another extension method to do that:

public static string ConvertXHTMLEntities(this string input)
{
    // Convert all ampersands to the ampersand entity.
    string output = input;
    output = output.Replace("&amp;", "amp_token");
    output = output.Replace("&", "&amp;");
    output = output.Replace("amp_token", "&amp;");

    // Convert less than to the less than entity (without messing up tags).
    output = output.Replace("< ", "&lt; ");
    return output;
}

现在我可以把用户提交的字符串,并检查它不使用以下code包含HTML:

Now I can take a user submitted string and check that it doesn't contain HTML using the following code:

bool ContainsHTML = UserEnteredString.ConvertXHTMLEntities().ContainsXHTML();

我不知道这是否是防弹的,但我认为这是不够好我的情况。

I'm not sure if this is bullet proof, but I think it's good enough for my situation.

这篇关于如何验证,使用C#中的字符串不包含HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆