Reputation: 4872
When i am doing Deserialize of xml i am getting "There is an error in XML document (1, 41)." . Can anyone tell me about what is the issue is all about.
public static T DeserializeFromXml<T>(string xml)
{
T result;
XmlSerializer ser = new XmlSerializer(typeof(T));
using (TextReader tr = new StringReader(xml))
{
result = (T)ser.Deserialize(tr);
}
return result;
}
I use this function to do it.
<?xml version='1.0' encoding='utf-16'?>
<Message>
<FirstName>Hunt</FirstName>
<LastName>DAvid</LastName>
</Message>
Upvotes: 24
Views: 161981
Reputation: 1
I was trying to de-serialize decimal into float, which was throwing the excpetion . Always try to look into inner exception as this is generic excpetion . Inner exception will give you the exact cause of issue . Happy Coding .
Upvotes: 0
Reputation: 2069
In my case I had a float value expected where xml had a null value so be sure to search for float and int data type in your xsd map
Upvotes: 1
Reputation: 1
On a WEC7 project I'm working on, I got a similar error. The file I was serializing in was serialized out from an array of objects, so I figured the XML was fine. Also, I have had this working for a few previous classes, so it was quite a puzzle.
Then I noticed in my earlier work that every class that I was serializing/deserializing had a default constructor. That was missing in my failed case so I added it and and voila... it worked fine.
I seem to remember reading somewhere that this was required. I guess it is.
Upvotes: 0
Reputation: 51
First check the variables declared using proper Datatypes. I had a same problem then I have checked, by mistake I declared SAPUser as int datatype so that the error occurred. One more thing XML file stores its data using concept like array but its first index starts having +1. e.g. if error is in(7,2) then check for 6th line always.....
Upvotes: 5
Reputation: 51
I had the same thing. All came down to a "d" instead of a "D" in a tag name in the schema.
Upvotes: 2
Reputation: 555
Agreed with the answer from sll, but experienced another hurdle which was having specified a namespace in the attributes, when receiving the return xml that namespace wasn't included and thus failed finding the class.
i had to find a workaround to specifying the namespace in the attribute and it worked.
ie.
[Serializable()]
[XmlRoot("Patient", Namespace = "http://www.xxxx.org/TargetNamespace")]
public class Patient
generated
<Patient xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.xxxx.org/TargetNamespace">
but I had to change it to
[Serializable()]
[XmlRoot("Patient")]
public class Patient
which generated to
<Patient xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
This solved my problem, hope it helps someone else.
Upvotes: 4
Reputation: 62514
Ensure your Message class looks like below:
[Serializable, XmlRoot("Message")]
public class Message
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
This works for me fine:
string xml = File.ReadAllText("c:\\Message.xml");
var result = DeserializeFromXml<Message>(xml);
The name of the XML root element that is generated and recognized in an XML-document instance. The default is the name of the serialized class.
So it might be your class name is not Message
and this is why deserializer was not able find it using default behaviour.
Upvotes: 23