How can i easily sanitize the values I pass into the Value property of an XAttribute.
+1
A:
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());
}
}
spender
2010-07-08 23:40:59
+1
A:
You could try:
string value = "!@#$%^&*()123%^&*(!@#\(*!&10987"
value = System.Security.SecurityElement.Escape(value);
This will escape characters that are invalid as XML attribute values.
Sean Amos
2010-07-08 23:55:19
Nice, but doesn't get rid of the \0.
spender
2010-07-08 23:58:23