5

I am trying to deserialize a xml string in C# using the following

XmlSerializer serializer = new XmlSerializer(typeof(Application));

App = (Application)serializer.Deserialize(xmlString);

It all works well when the xml is pretty printed, but when i have the whole xml in a single line deserializing fails with the error

There is an error in XML document (1, 2). Name cannot begin with the '.' character, hexadecimal value 0x00. Line 1, position 2."

I've checked that the xml is valid as such.

Any one knows what can be done to overcome this problem?

4
  • 3
    Can you post an example of the XML? Commented Apr 22, 2011 at 16:22
  • 1
    How did you determine that the XML is valid? Clearly, it contains a NULL (0x00) that you can't see. Find out why you can't see it. Commented Apr 22, 2011 at 16:24
  • i copied the string into xml spy and it doesn't complain, it even validates against the schema
    – user560174
    Commented Apr 22, 2011 at 16:29
  • it does not have a <?xml version="1.0"?> tag at the beginning though, but unfortunately i can't do anything about it, nor do i understand why that should be a problem
    – user560174
    Commented Apr 22, 2011 at 16:40

1 Answer 1

10

Chances are that you loading UTF-16 file as UTF-8 and as result every second character is 0.

If it is true - it could happen if you saved your original XML without BOM (byte order mark) or explicitly using wrong encoding while openeing file...

4
  • i am getting the xml string from a database so i cannot control that, is there a deserialization that can handle UTF-16 as well?
    – user560174
    Commented Apr 22, 2011 at 16:25
  • @user560174: How are you getting it from the database, as a string or as a sequence of bytes? Commented Apr 22, 2011 at 16:26
  • @user: How is the string stored in the database? what datatype is the column? Commented Apr 22, 2011 at 16:33
  • Post first several bytes of your "string", so we can confirm the encoding. I'd recommend create XmlReader and explicitly specifying "Unicode" encoding when you read this string as "Bytes". Commented Apr 22, 2011 at 21:26

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.