run time Exception when deserialize an Xml file

T

Tony Johansson

Hi!

I have this Xml file below and this small console program. At the end I have
class Movie. I want to serialize these three Movie object into a generic
collection of List<Movie>. When I run this program I get run time Exception
saying
"InvalidOperationException was unhandled There is an error in
XML-document(0,0)".

The xml document is well formed so it shouldn'y be any problem with it.
So can anybody tell me why the runtime complains about the XML file ?

static void Main(string[] args)
{
List<Movie> list = new List<Movie>();
FileStream fs = new FileStream("XMLFile2.xml", FileMode.Open);
XmlSerializer xs = new XmlSerializer(typeof(List<Movie>));
list = (List<Movie>)xs.Deserialize(fs);
}
<?xml version="1.0" encoding="utf-8" ?>
<movies date="2009-11-18">
<movie>
<title>My Sister's Keeper</title>
<runningLength>109</runningLength>
<productionYear>2009</productionYear>
<isbnNumber>9100120820</isbnNumber>
<url>http://images.filmtipset.se/posters/86432038.jpg</url>
</movie>
<movie>
<title>Fight Club</title>
<runningLength>139</runningLength>
<productionYear>1999</productionYear>
<isbnNumber>9100123064</isbnNumber>
<url>http://images.filmtipset.se/posters/93394736.jpg</url>
</movie>
<movie>
<title>Star Trek</title>
<runningLength>127</runningLength>
<productionYear>2009</productionYear>
<isbnNumber>9172321385</isbnNumber>
<url>http://images.filmtipset.se/posters/83532544.jpg</url>
</movie>
</movies>

using System;
using System.Xml.Serialization;
public class Movie
{
private string title = string.Empty;
private string url = string.Empty;
private string runningLength;
private string productionYear;
private string isbnNumber;

public string Title
{
get { return title; }
set { title = value; }
}
public string RunningLength
{
get { return runningLength; }
set { runningLength = value; }
}
public string ProductionYear
{
get { return productionYear; }
set { productionYear = value; }
}
public string IsbnNumber
{
get { return isbnNumber; }
set { isbnNumber = value; }
}
public string Url
{
get { return url; }
set { url = value; }
}
}

//Tony
 
F

Family Tree Mike

Tony Johansson said:
Hi!

I have this Xml file below and this small console program. At the end I have
class Movie. I want to serialize these three Movie object into a generic
collection of List<Movie>. When I run this program I get run time Exception
saying
"InvalidOperationException was unhandled There is an error in
XML-document(0,0)".

The xml document is well formed so it shouldn'y be any problem with it.
So can anybody tell me why the runtime complains about the XML file ?

static void Main(string[] args)
{
List<Movie> list = new List<Movie>();
FileStream fs = new FileStream("XMLFile2.xml", FileMode.Open);
XmlSerializer xs = new XmlSerializer(typeof(List<Movie>));
list = (List<Movie>)xs.Deserialize(fs);
}
<?xml version="1.0" encoding="utf-8" ?>
<movies date="2009-11-18">
<movie>
<title>My Sister's Keeper</title>
<runningLength>109</runningLength>
<productionYear>2009</productionYear>
<isbnNumber>9100120820</isbnNumber>
<url>http://images.filmtipset.se/posters/86432038.jpg</url>
</movie>
<movie>
<title>Fight Club</title>
<runningLength>139</runningLength>
<productionYear>1999</productionYear>
<isbnNumber>9100123064</isbnNumber>
<url>http://images.filmtipset.se/posters/93394736.jpg</url>
</movie>
<movie>
<title>Star Trek</title>
<runningLength>127</runningLength>
<productionYear>2009</productionYear>
<isbnNumber>9172321385</isbnNumber>
<url>http://images.filmtipset.se/posters/83532544.jpg</url>
</movie>
</movies>

using System;
using System.Xml.Serialization;
public class Movie
{
private string title = string.Empty;
private string url = string.Empty;
private string runningLength;
private string productionYear;
private string isbnNumber;

public string Title
{
get { return title; }
set { title = value; }
}
public string RunningLength
{
get { return runningLength; }
set { runningLength = value; }
}
public string ProductionYear
{
get { return productionYear; }
set { productionYear = value; }
}
public string IsbnNumber
{
get { return isbnNumber; }
set { isbnNumber = value; }
}
public string Url
{
get { return url; }
set { url = value; }
}
}

//Tony


.

Tony,

Was there an innerexception? That "usually" will point to more useful data
to fixing the problem when serialization/deserialization errors occur.

Mike
 
T

Tony Johansson

Family Tree Mike said:
Tony Johansson said:
Hi!

I have this Xml file below and this small console program. At the end I
have
class Movie. I want to serialize these three Movie object into a generic
collection of List<Movie>. When I run this program I get run time
Exception
saying
"InvalidOperationException was unhandled There is an error in
XML-document(0,0)".

The xml document is well formed so it shouldn'y be any problem with it.
So can anybody tell me why the runtime complains about the XML file ?

static void Main(string[] args)
{
List<Movie> list = new List<Movie>();
FileStream fs = new FileStream("XMLFile2.xml", FileMode.Open);
XmlSerializer xs = new XmlSerializer(typeof(List<Movie>));
list = (List<Movie>)xs.Deserialize(fs);
}
<?xml version="1.0" encoding="utf-8" ?>
<movies date="2009-11-18">
<movie>
<title>My Sister's Keeper</title>
<runningLength>109</runningLength>
<productionYear>2009</productionYear>
<isbnNumber>9100120820</isbnNumber>
<url>http://images.filmtipset.se/posters/86432038.jpg</url>
</movie>
<movie>
<title>Fight Club</title>
<runningLength>139</runningLength>
<productionYear>1999</productionYear>
<isbnNumber>9100123064</isbnNumber>
<url>http://images.filmtipset.se/posters/93394736.jpg</url>
</movie>
<movie>
<title>Star Trek</title>
<runningLength>127</runningLength>
<productionYear>2009</productionYear>
<isbnNumber>9172321385</isbnNumber>
<url>http://images.filmtipset.se/posters/83532544.jpg</url>
</movie>
</movies>

using System;
using System.Xml.Serialization;
public class Movie
{
private string title = string.Empty;
private string url = string.Empty;
private string runningLength;
private string productionYear;
private string isbnNumber;

public string Title
{
get { return title; }
set { title = value; }
}
public string RunningLength
{
get { return runningLength; }
set { runningLength = value; }
}
public string ProductionYear
{
get { return productionYear; }
set { productionYear = value; }
}
public string IsbnNumber
{
get { return isbnNumber; }
set { isbnNumber = value; }
}
public string Url
{
get { return url; }
set { url = value; }
}
}

//Tony


.

Tony,

Was there an innerexception? That "usually" will point to more useful
data
to fixing the problem when serialization/deserialization errors occur.

Mike

It's this row that trigger the exception.
list = (List<Movie>)xs.Deserialize(fs);

The text below is a copy of the exception detail
System.InvalidOperationException was unhandled
Message="There is an error in the XML-dokumentet (2, 2)."
Source="System.Xml"
StackTrace:
vid System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader
xmlReader, String encodingStyle, XmlDeserializationEvents events)
vid System.Xml.Serialization.XmlSerializer.Deserialize(Stream stream)
vid Program.Main(String[] args) i
F:\C#\ConsoleApplication1\ConsoleApplication1\Program.cs:rad 16
vid System.AppDomain._nExecuteAssembly(Assembly assembly, String[]
args)
vid System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence
assemblySecurity, String[] args)
vid Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
vid System.Threading.ThreadHelper.ThreadStart_Context(Object state)
vid System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state)
vid System.Threading.ThreadHelper.ThreadStart()

Do you have any suggestion what mignt be wrong ?

//Tony
 
T

Tony Johansson

Family Tree Mike said:
Tony Johansson said:
Hi!

I have this Xml file below and this small console program. At the end I
have
class Movie. I want to serialize these three Movie object into a generic
collection of List<Movie>. When I run this program I get run time
Exception
saying
"InvalidOperationException was unhandled There is an error in
XML-document(0,0)".

The xml document is well formed so it shouldn'y be any problem with it.
So can anybody tell me why the runtime complains about the XML file ?

static void Main(string[] args)
{
List<Movie> list = new List<Movie>();
FileStream fs = new FileStream("XMLFile2.xml", FileMode.Open);
XmlSerializer xs = new XmlSerializer(typeof(List<Movie>));
list = (List<Movie>)xs.Deserialize(fs);
}
<?xml version="1.0" encoding="utf-8" ?>
<movies date="2009-11-18">
<movie>
<title>My Sister's Keeper</title>
<runningLength>109</runningLength>
<productionYear>2009</productionYear>
<isbnNumber>9100120820</isbnNumber>
<url>http://images.filmtipset.se/posters/86432038.jpg</url>
</movie>
<movie>
<title>Fight Club</title>
<runningLength>139</runningLength>
<productionYear>1999</productionYear>
<isbnNumber>9100123064</isbnNumber>
<url>http://images.filmtipset.se/posters/93394736.jpg</url>
</movie>
<movie>
<title>Star Trek</title>
<runningLength>127</runningLength>
<productionYear>2009</productionYear>
<isbnNumber>9172321385</isbnNumber>
<url>http://images.filmtipset.se/posters/83532544.jpg</url>
</movie>
</movies>

using System;
using System.Xml.Serialization;
public class Movie
{
private string title = string.Empty;
private string url = string.Empty;
private string runningLength;
private string productionYear;
private string isbnNumber;

public string Title
{
get { return title; }
set { title = value; }
}
public string RunningLength
{
get { return runningLength; }
set { runningLength = value; }
}
public string ProductionYear
{
get { return productionYear; }
set { productionYear = value; }
}
public string IsbnNumber
{
get { return isbnNumber; }
set { isbnNumber = value; }
}
public string Url
{
get { return url; }
set { url = value; }
}
}

//Tony


.

Tony,

Was there an innerexception? That "usually" will point to more useful
data
to fixing the problem when serialization/deserialization errors occur.

Mike

Hi!

I have noticed that the node above the repeating node in this case the
repeating node is Movie. I have three Movies nodes.
So the node above this Movie node must be named ArrayOfMovie. So if I just
change node movies to ArrayOfMovie it work perfect.

Can somebody explain this ?



//Tony
 
F

Family Tree Mike

Tony Johansson said:
Family Tree Mike said:
Tony Johansson said:
Hi!

I have this Xml file below and this small console program. At the end I
have
class Movie. I want to serialize these three Movie object into a generic
collection of List<Movie>. When I run this program I get run time
Exception
saying
"InvalidOperationException was unhandled There is an error in
XML-document(0,0)".

The xml document is well formed so it shouldn'y be any problem with it.
So can anybody tell me why the runtime complains about the XML file ?

static void Main(string[] args)
{
List<Movie> list = new List<Movie>();
FileStream fs = new FileStream("XMLFile2.xml", FileMode.Open);
XmlSerializer xs = new XmlSerializer(typeof(List<Movie>));
list = (List<Movie>)xs.Deserialize(fs);
}
<?xml version="1.0" encoding="utf-8" ?>
<movies date="2009-11-18">
<movie>
<title>My Sister's Keeper</title>
<runningLength>109</runningLength>
<productionYear>2009</productionYear>
<isbnNumber>9100120820</isbnNumber>
<url>http://images.filmtipset.se/posters/86432038.jpg</url>
</movie>
<movie>
<title>Fight Club</title>
<runningLength>139</runningLength>
<productionYear>1999</productionYear>
<isbnNumber>9100123064</isbnNumber>
<url>http://images.filmtipset.se/posters/93394736.jpg</url>
</movie>
<movie>
<title>Star Trek</title>
<runningLength>127</runningLength>
<productionYear>2009</productionYear>
<isbnNumber>9172321385</isbnNumber>
<url>http://images.filmtipset.se/posters/83532544.jpg</url>
</movie>
</movies>

using System;
using System.Xml.Serialization;
public class Movie
{
private string title = string.Empty;
private string url = string.Empty;
private string runningLength;
private string productionYear;
private string isbnNumber;

public string Title
{
get { return title; }
set { title = value; }
}
public string RunningLength
{
get { return runningLength; }
set { runningLength = value; }
}
public string ProductionYear
{
get { return productionYear; }
set { productionYear = value; }
}
public string IsbnNumber
{
get { return isbnNumber; }
set { isbnNumber = value; }
}
public string Url
{
get { return url; }
set { url = value; }
}
}

//Tony


.

Tony,

Was there an innerexception? That "usually" will point to more useful
data
to fixing the problem when serialization/deserialization errors occur.

Mike

Hi!

I have noticed that the node above the repeating node in this case the
repeating node is Movie. I have three Movies nodes.
So the node above this Movie node must be named ArrayOfMovie. So if I just
change node movies to ArrayOfMovie it work perfect.

Can somebody explain this ?

The default for the root node is "ArrayOfMovie" if you have a List<Movie>
object being serialized. To use your original xml, change your code so the
XmlSerializer is constructed as follows:

XmlSerializer xs = new XmlSerializer(typeof(List<Movie>,
new XmlRootAttribute("movies"));

Also note that the xml file you have shown is using lowercase property
names, which don't agree with the class definition properies. You are
getting three Movie objects created but none of the attributes are being
attached. Your XML property names must match in case, or you must provide
the XmlElement attribute for each property specifying the name you wish to
use.

Mike
 
T

Tony Johansson

Family Tree Mike said:
Tony Johansson said:
Family Tree Mike said:
:

Hi!

I have this Xml file below and this small console program. At the end
I
have
class Movie. I want to serialize these three Movie object into a
generic
collection of List<Movie>. When I run this program I get run time
Exception
saying
"InvalidOperationException was unhandled There is an error in
XML-document(0,0)".

The xml document is well formed so it shouldn'y be any problem with
it.
So can anybody tell me why the runtime complains about the XML file ?

static void Main(string[] args)
{
List<Movie> list = new List<Movie>();
FileStream fs = new FileStream("XMLFile2.xml",
FileMode.Open);
XmlSerializer xs = new XmlSerializer(typeof(List<Movie>));
list = (List<Movie>)xs.Deserialize(fs);
}
<?xml version="1.0" encoding="utf-8" ?>
<movies date="2009-11-18">
<movie>
<title>My Sister's Keeper</title>
<runningLength>109</runningLength>
<productionYear>2009</productionYear>
<isbnNumber>9100120820</isbnNumber>
<url>http://images.filmtipset.se/posters/86432038.jpg</url>
</movie>
<movie>
<title>Fight Club</title>
<runningLength>139</runningLength>
<productionYear>1999</productionYear>
<isbnNumber>9100123064</isbnNumber>
<url>http://images.filmtipset.se/posters/93394736.jpg</url>
</movie>
<movie>
<title>Star Trek</title>
<runningLength>127</runningLength>
<productionYear>2009</productionYear>
<isbnNumber>9172321385</isbnNumber>
<url>http://images.filmtipset.se/posters/83532544.jpg</url>
</movie>
</movies>

using System;
using System.Xml.Serialization;
public class Movie
{
private string title = string.Empty;
private string url = string.Empty;
private string runningLength;
private string productionYear;
private string isbnNumber;

public string Title
{
get { return title; }
set { title = value; }
}
public string RunningLength
{
get { return runningLength; }
set { runningLength = value; }
}
public string ProductionYear
{
get { return productionYear; }
set { productionYear = value; }
}
public string IsbnNumber
{
get { return isbnNumber; }
set { isbnNumber = value; }
}
public string Url
{
get { return url; }
set { url = value; }
}
}

//Tony


.


Tony,

Was there an innerexception? That "usually" will point to more useful
data
to fixing the problem when serialization/deserialization errors occur.

Mike

Hi!

I have noticed that the node above the repeating node in this case the
repeating node is Movie. I have three Movies nodes.
So the node above this Movie node must be named ArrayOfMovie. So if I
just
change node movies to ArrayOfMovie it work perfect.

Can somebody explain this ?

The default for the root node is "ArrayOfMovie" if you have a List<Movie>
object being serialized. To use your original xml, change your code so
the
XmlSerializer is constructed as follows:

XmlSerializer xs = new XmlSerializer(typeof(List<Movie>,
new XmlRootAttribute("movies"));

Also note that the xml file you have shown is using lowercase property
names, which don't agree with the class definition properies. You are
getting three Movie objects created but none of the attributes are being
attached. Your XML property names must match in case, or you must provide
the XmlElement attribute for each property specifying the name you wish to
use.

Mike


Hi!

This work perfect now. Many thanks!
One more thing if I have an Xml file stored in this Url
http://midas.homelinux.net:8888/lectures/movies_tig076.xml
and I want to deserialize this according to what I have written earlier in
this thread. How can I best accomplish that. ?
I assume there is a big difference to have it local on the computer compared
to have it on this address.
http://midas.homelinux.net:8888/lectures/movies_tig076.xml

When I solved it I used DOM but it's also possible to use XmlReader but I
mean it would be great if I just could XML deserialized the three Movie
object into a collection of List<Movie>

//Tony
 
F

Family Tree Mike

Tony Johansson said:
Hi!

This work perfect now. Many thanks!
One more thing if I have an Xml file stored in this Url
http://midas.homelinux.net:8888/lectures/movies_tig076.xml
and I want to deserialize this according to what I have written earlier in
this thread. How can I best accomplish that. ?
I assume there is a big difference to have it local on the computer compared
to have it on this address.
http://midas.homelinux.net:8888/lectures/movies_tig076.xml

When I solved it I used DOM but it's also possible to use XmlReader but I
mean it would be great if I just could XML deserialized the three Movie
object into a collection of List<Movie>

//Tony


.

You can use WebClient.OpenRead("someUrl") to provide a stream to use in the
deserialize method. Specifically:

WebClient wc = new WebClient();
list = (List<Movie>) xs.Deserialize(wc.OpenRead
("http://midas.homelinux.net:8888/lectures/movies_tig076.xml"));
 
T

Tony Johansson

Family Tree Mike said:
You can use WebClient.OpenRead("someUrl") to provide a stream to use in
the
deserialize method. Specifically:

WebClient wc = new WebClient();
list = (List<Movie>) xs.Deserialize(wc.OpenRead
("http://midas.homelinux.net:8888/lectures/movies_tig076.xml"));

Hi!

This works but there is some major problems.
It's not possible to assign for example the first object in the collection
inList to a new object called mov1 as I have done below.
This collection inList behave very strange.
For example it's not possible to traverse throught the collection by using
foreach

WebClient wc = new WebClient();
XmlSerializer xs = new XmlSerializer(typeof(List<movie>), new
XmlRootAttribute("movies"));
List<movie> inList = (List<movie>)xs.Deserialize(wc.OpenRead(
("http://midas.homelinux.net:8888/lectures/movies_tig076.xml"));
movie mov1 = inList[0];

//Tony
 
T

Tony Johansson

Tony Johansson said:
Family Tree Mike said:
You can use WebClient.OpenRead("someUrl") to provide a stream to use in
the
deserialize method. Specifically:

WebClient wc = new WebClient();
list = (List<Movie>) xs.Deserialize(wc.OpenRead
("http://midas.homelinux.net:8888/lectures/movies_tig076.xml"));

Hi!

This works but there is some major problems.
It's not possible to assign for example the first object in the collection
inList to a new object called mov1 as I have done below.
This collection inList behave very strange.
For example it's not possible to traverse throught the collection by using
foreach

WebClient wc = new WebClient();
XmlSerializer xs = new XmlSerializer(typeof(List<movie>), new
XmlRootAttribute("movies"));
List<movie> inList = (List<movie>)xs.Deserialize(wc.OpenRead(
("http://midas.homelinux.net:8888/lectures/movies_tig076.xml"));
movie mov1 = inList[0];

//Tony

I works now. I had done something strange.

//Tony
 

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

Similar Threads


Top