-1

I am invoking a service that returns responses as xml format. The response doesnt follow the xml guidelines and contains some new lines and "\".

Due to the formatting issues, the deserialization is failing.

XML Format:

\r\n\r\n<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<N><details><date>25042014</date><orderNumber>OrderNumber         </orderNumber><Response>1</Response></details>

I worked around the problem by removing the new lines and "\" before deserialization but was searching for a cleaner solution if exists.

Hussein Zawawi
  • 2,907
  • 2
  • 26
  • 44
  • Best solution would probably be to use a built-in serializer/deserializer. These engines already takes care of this for you, and can handle most cases very well. – Falgantil May 12 '14 at 13:43
  • I am using the built-in deserializer but am getting the issue there. – Hussein Zawawi May 12 '14 at 13:51

1 Answers1

1

The XML file has to be well defined, so it must be corresponding to an XSD structure. The escape sequences and new lines will destroy the valid xml, and thus will not correspond to the XSD structure, which, in turn, will cause the deserialization to fail. As far as I know, there is no way around it, except to read the file beforehand, remove the unwanted characters and sequences, and saving it again, so that it may be successfully deserialized when read by an XmlDocument.

Alex Barac
  • 632
  • 4
  • 12
  • Just to add: reading, resaving as a file, then re-reading may be unnecessary. Depending on HusseinX's serialization layer, the changes may likely be made in-memory by reading in the file/stream as a `string`, manipulating that string, and then feeding it into the deserializer mechanism. – Chris Sinclair May 12 '14 at 13:51
  • I tried also to try XML parsing by I got the same issue. So with this format there is no way to do it unless I adjust the formatting? – Hussein Zawawi May 12 '14 at 14:08
  • @HusseinX I also confronted the same problem a couple of days ago, and this was the solution I came up with. It is possible to just work with the memory, as Chris Sinclair mentioned, but saving the modified file to hard drive and re-reading the file is a good solution, especially if you wish to work with the valid file later. – Alex Barac May 12 '14 at 15:25
  • @AlexBarac I am getting this response from a remote service, I dont have any local files. I used to manipulate the xml before deserialized in the memory but was searching for other suggestions. Thanks – Hussein Zawawi May 12 '14 at 15:46