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.
精彩评论