Sunday, 11 January 2015

De\Serializing list of objects XML



I have been tried to do serialize to list of object. all I get in the XML file is :



<?xml version="1.0" encoding="utf-8"?>
<ArrayOfObserver xmlns:xsi="http://ift.tt/ra1lAU" xmlns:xsd="http://ift.tt/tphNwY">
<Observer />
<Observer />
</ArrayOfObserver>


it shows that the observer is empty but the list that i send to him has 2 object. why it happen ? what to do ? what wrongs ?


My code :


in the main :



Serialize(lstObservers);

public void Serialize(List<Observer> list)
{
XmlSerializer serializer = new XmlSerializer(typeof(List<Observer>));
using ( TextWriter writer = new StreamWriter( @"C:\Users\user\Desktop\MapSample\bin\Debug\ListObserver.xml"))
{
serializer.Serialize(writer, list);
}
}


the class observer :



[Serializable()]
public class Observer : MapObject
{
private int ID_Observer { get; set; }
private double azimuth;
private double Long;
private double Lat;
private double Lenght;
private bool haveConnection;
private bool DrawAzimuth;
private XmlSerializer ser;

/// <summary>
/// C'tor
/// </summary>
public Observer(int ID_Observer = 0, double azimuth = 0, double Lat = 0, double Long = 0, double Lenght = 0, bool haveConnection = true, bool DrawAzimuth = true)
{
this.ID_Observer = ID_Observer;
this.azimuth = azimuth;
this.Long = Long;
this.Lat = Lat;
this.haveConnection = haveConnection;
this.DrawAzimuth = DrawAzimuth;
this.Lenght = Lenght;
}

public Observer()
{
ser = new XmlSerializer(this.GetType());
}
}


EDIT : my propose is to save list of objects.. if anyone have another idea i will be more than happy to learn. Thanks


No comments:

Post a Comment