2

I have a 2D array (a matrix) that I code to xml using XmlSerializer. When I de-serialize the string I get errors sometimes.
Like the following. It says XML-Document is bad because it contains a hex value 0x00.

System.InvalidOperationException: Fehler im XML-Dokument (1,18449). ---
    System.Xml.XmlException: '.', hexidezimaler Wert 0x00, ist ein ungültiges 
    Zeichen. Zeile 1, Position 18449.
bei System.Xml.XmlTextReaderImpl.Throw(Exception e)
bei System.Xml.XmlTextReaderImpl.Throw(String res, String[] args)
bei System.Xml.XmlTextReaderImpl.Throw(Int32 pos, String res, String[] args)


Possible solution

The error parts is this:

<anyType xsi:type="xsd:string">&#x0;</anyType>

Does that mean &#x0; is not serializable or is the string I'm trying to serialize bad ? What can I do to avoid having this exception ?


My code:
// serialize

StringBuilder builder = new StringBuilder(512 * 1024);
StringWriter writer = new StringWriter(builder);
object[][] array = table.ToArray(true);
try
{
    new XmlSerializer(typeof(object[][])).Serialize(writer, array);
    return builder.Replace(Environment.NewLine, string.Empty).ToString();
}
catch (Exception exception) { return exception.Message; }
finally
{
    writer.Close();
    writer.Dispose();
}

// de-serialize

StringReader reader = new StringReader(xml);
object[][] array = (object[][])new XmlSerializer(typeof(object[][])).Deserialize(reader);
reader.Close();
reader.Dispose();
Community
  • 1
  • 1
Bitterblue
  • 13,162
  • 17
  • 86
  • 124
  • XML does not allow an entity with value 0. [spec](http://www.w3.org/TR/REC-xml/#charsets) – knittl Sep 26 '14 at 13:16
  • The NUL teminator character is [not legal in XML at all](http://www.w3.org/TR/REC-xml/#NT-Char) – pln Sep 26 '14 at 13:17
  • possible duplicate of [The Invulnerable XMLException](http://stackoverflow.com/questions/9798033/the-invulnerable-xmlexception) – ale Sep 26 '14 at 13:18
  • I'm curious why serializing your objects is resulting in ``. What kind of values are stored in your array? – JLRishe Sep 26 '14 at 13:24
  • @JLRishe It's from a DB. – Bitterblue Sep 26 '14 at 13:26
  • @Bitterblue So what kind of values are in it? Strings? Numbers? Other stuff? – JLRishe Sep 26 '14 at 13:27
  • @JLRishe It's a string. I just checked and it is a string containing the char `'\0'`. If I replace it with something else like space, serialize it, de-serialize it the error is gone. – Bitterblue Sep 26 '14 at 13:39
  • possible duplicate of [Why are "control" characters illegal in XML?](http://stackoverflow.com/questions/404107/why-are-control-characters-illegal-in-xml) – Joe Sep 27 '14 at 12:49

1 Answers1

3

Yes, there is a whole bunch of characters that are not allowed in XML documents, and ASCII code 0 is one of them. (Thank you to knittl and pln for providing the location in the spec).

Is there a reason that your string contains the hex value 0x00? That seems like a strange value for a string to have, since it's used as a termination character.

I would recommend having a look at that, and if the string does indeed need to contain these characters, one option you have is to convert the data to base64 during serialization, and convert it back during deserialization.

JLRishe
  • 99,490
  • 19
  • 131
  • 169