mercoledì 10 agosto 2011 #

HTTP Listener and XML

Today, working at a small project using the .Net object in the title, we found something weird; if I used an HTTP Get to send a string to the Listener and the string was a plain text like “Hello, I am the string”, it arrived exactly as it was sent, while if the string was the XML serialization of a custom object the result string was different when received and before the <?xml we found three characters, an i with an umlaot, a closing double quote, and an upside down question mark.
Digging into the string we found that the XML Serialization by default inserts as first character of a serialized string/file the unicode character  \UFEFF, which is a non visible character (also notepad does not show it), this character is necessary to the device/software/whatever that receives the text to determine if it is BigEndian or LittleEndian, You can find the details here on Wikipedia if you are curious. This unicode character can be omitted in serialization just asking it to the .NET serializer, changing a property in the XmlWriterSettings that you give to the XmlSerializer the property is Encoding=false to exclude the character.

It is not exactly a solution, but it is a good workaround if you cannot do more. The real solution is call an UrlEncoding of the XML string before sending it using the GET, or else, if you want to be sure everything remains unchanged, you can convert the XML string to BASE64 Using Encoding.Unicode to obtain a Byte Array from the string and then parse it when received from the listener.

Usually the exception you receive when this problem happens is the following:

System.Xml.XmlException
Data at the root level is invalid. Line 1, position 1.

here is the screenshot of the XML string with the problem.

xml_err

Tag di Technorati: ,,,

posted @ mercoledì 10 agosto 2011 16.48 | Feedback (0)

Copyright © Sabrina C.

Design by Bartosz Brzezinski

Design by Phil Haack Based On A Design By Bartosz Brzezinski