Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XmlWriter.WriteCData throws an ArgumentException - invalid character

Tags:

c#

.net

xml

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.

like image 546
James Avatar asked Feb 03 '11 23:02

James


2 Answers

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.

like image 96
Jon Skeet Avatar answered Sep 21 '22 14:09

Jon Skeet


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.

like image 32
gbvb Avatar answered Sep 21 '22 14:09

gbvb