is it really possible to XML serialize without having acces to the collection

T

Tony Johansson

Hello!

Here I have some code. The first event handler MenuFileExportToXML_Click is
located in the GUI class and looks like this
private void MenuFileExportToXML_Click(object sender, EventArgs e)
{
try
{
XMLSerialization.XMLSerialize<Animal[]>(XMLFile,
animalManager.Animals.ToArray(), animalManager.ExtraTypes);
}
catch (IOException ex)
{
MessageBox.Show("An IOException occured " + ex.Message);
}
catch (Exception ex)
{
MessageBox.Show("An Exception occured " + ex.Message);
}
}
}

as you can see this will XML serialize the collection that is found when
calling animalManager.Animals.ToArray().
This animalManager.Animals.ToArray() is found in the AnimalManager class and
here is an extract from this class and looks like this
public class AnimalManager
{
...
private List<Animal> animals = new List<Animal>();

...

public IList<Animal> Animals
{
get { return animals.AsReadOnly(); }
}
...
}

as you can see I have a public property Animals that return an
IList<Animal>

Now to my question I must as I understand keep the access to the animal
collection public even if it's readonly because otherwise it would not be
possible to access it and serialize the collection animals.

So as a summary if there is no public access to the collection these is not
possible to XML serialize the animal collection that is located in the
AnimalManager class.

Is this correct understood ?

//Tony
 
A

Arne Vajhøj

Here I have some code. The first event handler MenuFileExportToXML_Click is
located in the GUI class and looks like this
private void MenuFileExportToXML_Click(object sender, EventArgs e)
{
try
{
XMLSerialization.XMLSerialize<Animal[]>(XMLFile,
animalManager.Animals.ToArray(), animalManager.ExtraTypes);
}
catch (IOException ex)
{
MessageBox.Show("An IOException occured " + ex.Message);
}
catch (Exception ex)
{
MessageBox.Show("An Exception occured " + ex.Message);
}
}
}

as you can see this will XML serialize the collection that is found when
calling animalManager.Animals.ToArray().
This animalManager.Animals.ToArray() is found in the AnimalManager class and
here is an extract from this class and looks like this
public class AnimalManager
{
...
private List<Animal> animals = new List<Animal>();

...

public IList<Animal> Animals
{
get { return animals.AsReadOnly(); }
}
...
}

as you can see I have a public property Animals that return an
IList<Animal>

Now to my question I must as I understand keep the access to the animal
collection public even if it's readonly because otherwise it would not be
possible to access it and serialize the collection animals.

So as a summary if there is no public access to the collection these is not
possible to XML serialize the animal collection that is located in the
AnimalManager class.

Is this correct understood ?

I think so.

XmlSerializer serializes properties.

Binary and SOAP serialization uses a different technique.

Arne
 

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