开发者

Sanitize XML Attribute Values

How can i easily sanitize the values I pass into the Value property of an XAttri开发者_如何学Pythonbute.


Here's an extension method to clean away your trouble. /0 is not allowed in XML. I'm not sure if other chars are also disallowed, but I believe not. Probably best to start at ' '.

void Main()
{

    Console.WriteLine("123\0394".XmlSanitize());

}

public static class XmlHelpers
{
    public static string XmlSanitize(this string badString)
    {
        return new String(badString.Where(c => c >=' ').ToArray());
    }
}


You could try:

string value = "!@#$%^&*()123%^&*(!@#\(*!&10987"
value = System.Security.SecurityElement.Escape(value);

This will escape characters that are invalid as XML attribute values.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜