21

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>
4
  • 21
    You might want to include (a part of) that XML document, especially line 1 position 41. Mar 22, 2012 at 11:51
  • 1
    It will help if you could copy the whole error message and put it as part of your question.
    – Julius A
    Mar 22, 2012 at 11:52
  • And also which tool you use to deserialise the xml.
    – Mr Lister
    Mar 22, 2012 at 11:54
  • 2
    Have you specified XmlRoot("Message")] for the class of T you are using when deserializing???
    – sll
    Mar 22, 2012 at 12:13

7 Answers 7

22

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);

MSDN, XmlRoot.ElementName:

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.

1
  • Thank you! I've realized that I must always set the property ElementName within XmlRoot attribute, because it's not known ahead of time whether the the class' name will be changed in the process of refactoring.
    – AlexMelw
    Nov 12, 2017 at 18:05
4

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.

4

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.....

2

I had the same thing. All came down to a "d" instead of a "D" in a tag name in the schema.

1

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

0

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.

0

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 .

1
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Sep 5 at 17:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.