I get exception if in XElement
's content I include characters such as '\x1A', '\x1B', '\x1C', '\x1D', '\x1E' or '\x1F'.
using System;
using System.Collections.Generic;
using System.Xml.Linq;
namespace LINQtoXMLInvalidChars
{
class Program
{
private static readonly IReadOnlyCollection<char> InvalidCharactersInXml = new List<char>
{
'<',
'>',
'&',
'\'',
'\"',
'\x1A',
'\x1B',
'\x1C',
'\x1D',
'\x1E',
'\x1F'
};
static void Main()
{
foreach (var c in InvalidCharactersInXml)
{
var xEl = new XElement("tag", "Character: " + c);
var xDoc = new XDocument(new XDeclaration("1.0", "utf-8", null), xEl);
try
{
Console.Write("Writing " + c + ": ");
Console.WriteLine(xDoc);
}
catch (Exception e)
{
Console.WriteLine("Oops. " + e.Message);
}
}
Console.ReadKey();
}
}
}
In an answer from Jon Skeet to the question String escape into XML I read
You set the text in a node, and it will automatically escape anything it needs to.
So now I'm confused. Do I misunderstand something?
Some background information: The string content of the XElement
comes from the end user. I see two options for making my application robust: 1) to Base-64 encode the string before passing it in to XElement
2) to narrow the accepted set of characters to e.g. alphanumeric characters.