XmlReader error Root Element is missing

S

sjoshi

I'm trying to read a simple xml file that I created but the XmlReader
keeps choking with this message

There is an error in XML document (0, 0).
----> System.Xml.XmlException : Root element is missing.

The code to read is

private static T ReadFromXml<T>(Stream s) where T: new()
{
T rVal = default(T);
XmlReaderSettings settings = new XmlReaderSettings();
settings.ConformanceLevel = ConformanceLevel.Auto;

using (XmlReader reader = XmlReader.Create(s, settings))
{
XmlSerializer serializer = new
XmlSerializer(typeof(T));
rVal = (T)serializer.Deserialize(s);
}

return rVal;
}

The xml files looks like this...

<?xml version="1.0" encoding="utf-8"?>
<Book xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<id>123</id>
<title>The way of prana and qi</title>
<author>Sunit Joshi</author>
<genre>Metaphysical</genre>
<price>21.99</price>
<publish_date>11/21/2007 4:19 PM</publish_date>
<description>An in-depth look at life energy</description>
</Book>

Any ideas what is happening here...

thanks
Sunit
 
S

sjoshi

I'm trying to read a simple xml file that I created but the XmlReader
keeps choking with this message

There is an error in XML document (0, 0).
----> System.Xml.XmlException : Root element is missing.

The code to read is

private static T ReadFromXml<T>(Stream s) where T: new()
{
T rVal = default(T);
XmlReaderSettings settings = new XmlReaderSettings();
settings.ConformanceLevel = ConformanceLevel.Auto;

using (XmlReader reader = XmlReader.Create(s, settings))
{
XmlSerializer serializer = new
XmlSerializer(typeof(T));
rVal = (T)serializer.Deserialize(s);
}

return rVal;
}

The xml files looks like this...

<?xml version="1.0" encoding="utf-8"?>
<Book xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<id>123</id>
<title>The way of prana and qi</title>
<author>Sunit Joshi</author>
<genre>Metaphysical</genre>
<price>21.99</price>
<publish_date>11/21/2007 4:19 PM</publish_date>
<description>An in-depth look at life energy</description>
</Book>

Any ideas what is happening here...

thanks
Sunit

Sorry my mistake...the line

rVal = (T)serializer.Deserialize(s);

should read

rVal = (T)serializer.Deserialize(reader);

thanks
Sunit;
 
M

Marc Gravell

Sorry my mistake...

Just to be clear - does this fix your problem, or is this just
correcting a typo in the demo (failing) code?

Marc
 
S

sjoshi

Just to be clear - does this fix your problem, or is this just
correcting a typo in the demo (failing) code?

Marc

It was a typo in my code that caused it to fail. After the change it
works fine now.

thanks & rgds
Sunit
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top