about xml serialization

T

Tony Johansson

Hi!

I have a collection of Movie object stored in a generic list named myList.
private List<Movie> myList = new List<Movie>();

Now I want to XML Serialize this generic list but that doesn't work because
I get Exception se below
Here is my attempt.
private void BtnSeralizeTransformedXML_Click(object sender, EventArgs e)
{
FileStream fs = new FileStream("Test.XML", FileMode.Create);
XmlSerializer xs = new XmlSerializer(typeof(Movie));
xs.Serialize(fs, myList);
fs.Close();
}

The runtime Exception I get is this
"InvalidOperationException was unhandled
it was not possible to generate the XML- document"

So why is it not possible to serialize the whole collection of Movie objects
?

[Serializable]
public class Movie
{
private string title = string.Empty;
private string url = string.Empty;
private int runningLength;
private int productionYear;
private long isbnNumber;

public string Title
{
get { return title; }
set { title = value; }
}

public int RunningLength
{
get { return runningLength; }
set { runningLength = value; }
}

public int ProductionYear
{
get { return productionYear; }
set { productionYear = value; }
}

public long IsbnNumber
{
get { return isbnNumber; }
set { isbnNumber = value; }
}

public string Url
{
get { return url; }
set { url = value; }
}
}

//Tony
 
F

Family Tree Mike

void BtnSeralizeTransformedXML_Click(object sender, EventArgs e)
{
FileStream fs = new FileStream("Test.XML", FileMode.Create);
XmlSerializer xs = new XmlSerializer(typeof(Movie));
xs.Serialize(fs, myList);
fs.Close();
}

You have created the serializer to serialize an object which is a movie,
but then called it to serialize a list of movies (myList).
 
M

Mr. Arnold

Tony said:
Hi!

I have a collection of Movie object stored in a generic list named myList.
private List<Movie> myList = new List<Movie>();

Now I want to XML Serialize this generic list but that doesn't work because
I get Exception se below
Here is my attempt.
private void BtnSeralizeTransformedXML_Click(object sender, EventArgs e)
{
FileStream fs = new FileStream("Test.XML", FileMode.Create);
XmlSerializer xs = new XmlSerializer(typeof(Movie));
xs.Serialize(fs, myList);
fs.Close();
}

The runtime Exception I get is this
"InvalidOperationException was unhandled
it was not possible to generate the XML- document"

So why is it not possible to serialize the whole collection of Movie objects
?

http://dotnetperls.com/serialize-list-tutorial
 
T

Tony Johansson

Family Tree Mike said:
You have created the serializer to serialize an object which is a movie,
but then called it to serialize a list of movies (myList).

I want to XML serialize a generic collection List<Movie> which is called
myList.
Do you have any suggestion ?
I got a url to a site but that was binary serialization which is not to much
use for me.

//Tony
 
F

Family Tree Mike

I want to XML serialize a generic collection List<Movie> which is called
myList.
Do you have any suggestion ?
I got a url to a site but that was binary serialization which is not to much
use for me.

//Tony

Just change your routine as follows:

void BtnSeralizeTransformedXML_Click(object sender, EventArgs e)
{
FileStream fs = new FileStream("Test.XML", FileMode.Create);
XmlSerializer xs = new XmlSerializer(typeof(List<Movie>));
xs.Serialize(fs, myList);
fs.Close();
}
 
T

Tony Johansson

Just change your routine as follows:
void BtnSeralizeTransformedXML_Click(object sender, EventArgs e)
{
FileStream fs = new FileStream("Test.XML", FileMode.Create);
XmlSerializer xs = new XmlSerializer(typeof(List<Movie>));
xs.Serialize(fs, myList);
fs.Close();
}

It works Now

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

Top