Escape Syntax Madness
Saturday, February 5, 2005
Have you ever used VBA to generate C# code that generates XML that includes quotes in attribute values? I did so in a somewhat obscure situation, which involved manipulating an XML file using rules laid down in an Excel sheet.
Here's a simplified demo. To have an XML attribut named "project" with a value like:
"WebEdit"
... you need:
project=""WebEdit""
... which can be generated like this:
Console.WriteLine("project=\""WebEdit"\"");
... which can be generated like this:
Debug.Print "Console.WriteLine(""project=\"""WebEdit"\"""");"
The combination of various escape methods drove me nuts. I took a pass on properly escaping the quotes in this document's HTML (except for &).