StringBuilder output = new StringBuilder();
using (XmlWriter writer = XmlWriter.Create(output))
{
writer.WriteStartElement("test");
writer.WriteCData("taco\vbell");
writer.WriteEndElement();
writer.WriteEndDocument();
}
Console.WriteLine(output.ToString());
WriteCData throws the following ArgumentException, "'\v', hexadecimal value 0x0B, is an invalid character"
I thought CData can take any kind of data. Since this is not the case, what characters do I have to escape? Thanks.
No, XML itself cannot represent any characters earlier than U+0020 other than tab, carriage return, and line feed.
From the spec, section 2.2:
Character Range
Char ::= #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF] /* any Unicode character, excluding the surrogate blocks, FFFE, and FFFF. */
There's no standard way of representing the "forbidden" characters, unfortunately. You'd have to create your own escaping mechanism.
I would recommend checking out http://www.w3.org/TR/REC-xml/#dt-cdsection for the specific characters allowed. That will show you what is allowed within XML (and that will show you that 0x0B is not allowed). Do you require this content to be maintained as is? If not, I would recommend Base64 encoding that so that you can be safe.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With