Solved

"There is an error in XML document (0, 0)." Root element is missing

Posted on 2013-05-19
5
4,162 Views
Last Modified: 2013-05-27
I get exception "There is an error in XML document (0, 0)." Root element is missing while deserializing an xml string. I give the code below.

What could be reason for the exception and how to resolve it? Please do assist.


 
  public static object FromXml(string xmlString, Type type)
        {
            object objectToReturn = null;
            var serializer = new XmlSerializer(type);
            using (var stream = new StringReader(xmlString))
            {
                try
                {
                    objectToReturn = serializer.Deserialize(stream);
                }
                catch (Exception ex)
                {
                    throw new InvalidOperationException("Failed to " + "create object from xml string", ex);
                }
            }

            return objectToReturn;
        }

Open in new window



The xml file looks like below:

<?xml version="1.0"?><SettlementInfo><Id>10000019</Id><Name>Easwar</Name>.....</SettlementInfo>

Open in new window

0
Comment
Question by:Easwaran Paramasivam
  • 2
  • 2
5 Comments
 
LVL 42

Accepted Solution

by:
sedgwick earned 400 total points
ID: 39178570
what is the type passed to the function?
try this generix xml deserializer instead:
 public static T DeserializeFromXml<T>(string xml)
        {
            T result;

            var ser = new XmlSerializer(typeof(T));
            using (var tr = new StringReader(xml))
            {
                result = (T)ser.Deserialize(tr);
            }
            return result;
        }

Open in new window

0
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 100 total points
ID: 39178590
Are you certain that is the XML being passed to the serializer? Have you tried stepping through that bit of code and inspecting your xmlString variable to see that is correct?
0
 
LVL 16

Author Comment

by:Easwaran Paramasivam
ID: 39198584
In the XML file I've replaced SettlementInfo with VIPSettlementInfo and it works fine.
0
 
LVL 16

Author Closing Comment

by:Easwaran Paramasivam
ID: 39198586
Your information is useful.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39199235
It would tickle me to no end to know how making the method generic solved the issue, especially considering that the algorithm is exactly the same.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question