Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2013-05-19
5
Medium Priority
?
4,729 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 42

Accepted Solution

by:
sedgwick earned 1200 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 300 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

688 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