XML : Why does LINQ to XML not escape characters like '\x1A'?

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?

No comments:

Post a Comment