开发者

Sanitize XML Attribute Values

开发者 https://www.devze.com 2023-01-06 23:35 出处:网络
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

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

精彩评论

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