views:

668

answers:

3

Im trying to deserialize an XML file with XmlSerializer, however im getting this exception:

"There is an error in XML document (1, 2)" The innerexception is: "<Mymessage xmlns='http://MyMessages/'&gt; was not expected."

Which is the very first line in the XML file. my guess is that it has something to do with the xmlns.

I tried to ask Google, and then tried to add the following line to my code

[XmlRoot("MyMessage", Namespace="'http://MyMessages/")]

But I still get the same exception.

+1  A: 

Please provide the full XML file code to help understand the issue better.

Also put this as the first line in the xml file and see if this solves the issue

 <?xml version="1.0" encoding="utf-8"?>
Binoj Antony
I would strongly recommend to use "utf-8" instead of iso-8859-1 for all interopertable web service work!
marc_s
+1  A: 

It sounds like you have a borked xml file. Easy ways to find out:

  • try loading it into an xml viewer
  • or just make sure it has a .xml extension and load in VS or IE
  • or run xsd.exe over it

If they complain, then the xml is certainly corrupt. If they work fine, and display your data, then you probably have the serialization attributes wrong. Try using xsd.exe with the "/classes" switch to see what it would do with it...

Marc Gravell
+1  A: 

In the constructor of the XmlSerializer i needed to specify a default namespace, after doing that everything worked just fine

CruelIO