locked
'', hexadecimal value 0x0E, is an invalid character

    Question

  •  

    Hi,

     

    I'm using LINQ to SQL to get some data from Company table, the database is Unicode. After a few hours found out the record in the DB causing this error message, when the ToString() methos is called of the object XDoducment/XElement.

     

    I'm able to get the same record using the Dataset and when I serialize the dataset, the XML file is generated without any errors and I could view that file in IE but if I try to load the same xml file using XDocument.load I get this message.

     

    When the XML file is view in notepad these are the chars 

     

    Boxes appear when viewed in IE.

     

    Upon further investigation those chars are shiftout (14) and shiftin (15).

     

    What am I missing here? Why is it failing with LINQ?

     

    Thanks,

    Saqib

    Tuesday, December 30, 2008 2:45 AM

Answers

  • XML does not allow most control characters. If you nevertheless want to use them then MS provides a workaround, you need to use an XmlWriter where CheckCharacters is set to false. Here is an example:

     

    Code Snippet

                XElement foo = new XElement("foo", "\u000E\u000F");
                XmlWriterSettings settings = new XmlWriterSettings();
                settings.CheckCharacters = false;
                StringWriter sw = new StringWriter();
                using (XmlWriter writer = XmlWriter.Create(sw, settings))
                {
                    foo.Save(writer);
                }
                string xml = sw.ToString();
                sw.Close();
                Console.WriteLine(xml);

     

     

    The result looks as follows:

    Code Snippet

    <?xml version="1.0" encoding="utf-16"?><foo>&#xE;&#xF;</foo>

     

     

    I am not sure why you don't run into the problem with a DataSet, it might be that a DataSet, for compatibility with.NET 1.x, by default uses an XmlWriter that does not perform character checks.
    Tuesday, December 30, 2008 12:38 PM

All replies

  • XML does not allow most control characters. If you nevertheless want to use them then MS provides a workaround, you need to use an XmlWriter where CheckCharacters is set to false. Here is an example:

     

    Code Snippet

                XElement foo = new XElement("foo", "\u000E\u000F");
                XmlWriterSettings settings = new XmlWriterSettings();
                settings.CheckCharacters = false;
                StringWriter sw = new StringWriter();
                using (XmlWriter writer = XmlWriter.Create(sw, settings))
                {
                    foo.Save(writer);
                }
                string xml = sw.ToString();
                sw.Close();
                Console.WriteLine(xml);

     

     

    The result looks as follows:

    Code Snippet

    <?xml version="1.0" encoding="utf-16"?><foo>&#xE;&#xF;</foo>

     

     

    I am not sure why you don't run into the problem with a DataSet, it might be that a DataSet, for compatibility with.NET 1.x, by default uses an XmlWriter that does not perform character checks.
    Tuesday, December 30, 2008 12:38 PM
  •  

    Thanks Martin! that did the trick. In my opinion it should work like the DataSet does by default and if someone wants to perform character check then turn it to true.

     

    Tuesday, December 30, 2008 4:35 PM