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

  • Thread starter Thread starter Tony Johansson
  • Start date Start date
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
 
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
 
Back
Top