Here’s some C# code:
var sb = new StringBuilder(); var w = XmlWriter.Create(sb); w.WriteStartElement('hello'); w.WriteAttributeString('target', 'world ' \' !'); w.WriteEndElement(); w.Flush(); // then look at sb.ToString()
I’m getting a string that looks like:
<?xml version='1.0' encoding='utf-16'?><hello target='world ' " !' />
It’s only escaping the double-quote, not the single-quote. But the docs for XmlWriter.WriteAttributeString(String, String) say:
If the attribute value includes double or single quotes, they are replaced with " and ' respectively.
Is there some flag I need to set to make it do what the docs say it does?
If you use .Net Reflector to look at the code, you’ll find that the System.Xml.XmlTextEncoder.Write(string) method is being called. Here’s the code of interest:
When writing an attribute value, a single quote is not escaped since it doesn’t need to be. It’s only when writing a text element that ‘'’ is used.