Hi,
I am trying to use Xml Serialization in order to serialize and deserialize an object for a webservice and for a database operations.
Here is an example of the class structure:
public abstract class Forniture {
private float m_Price;
public abstract int color{get; set;}
[XmlElement(DataType="float"))]
public float price{
get{return m_Price;}
set{m_Price=value;}
}
...
...
...
}
public class Chair : Forniture {
private float m_Price;
[XmlElement(DataType="int"))]
public override int color{
get {return 1;}
set {m_Color=value;}
}
...
...
...
}
public class Table : Forniture {
private float m_Price;
[XmlElement(DataType="int"))]
public override int color{
get {return 2;}
set {m_Color=value;}
}
...
...
...
}
public class Closet : Forniture {
private float m_Price;
[XmlElement(DataType="int"))]
public override int color{
get {return 3;}
set {m_Color=value;}
}
...
...
...
}
public class Room {
private List<Forniture> m_Fornitures;
[XmlArray(ElementName="forni"),
XmlArrayItem(Type=typeof(Fornitures), ElementName="item")]
public Forniture[] Fornitures {
get {return m_Fornitures.ToArray();}
set {m_Fornitures=new List<Forniture>(value);}
}
public Room() {
m_Fornitures=new List<Forniture>();
}
public void AddForniture(Forniture f) {
m_Fornitures.add(f);
}
}
public class Bathroom:Room {
private string m_member1;
private string m_member2;
[XmlElement(DataType="string"))]
public string Member1 {
get {return m_member1;}
set {m_member1=value;}
}
[XmlElement(DataType="string"))]
public string Member2 {
get {return m_member1;}
set {m_member2=value;}
}
public Bathroom():base() {
m_member1="214";
m_member2="sasdd";
AddForniture(new Closet());
AddForniture(new Table());
}
}
When serialize and deserialize object Bathroom, the problems are:
1. When asp.net deserialize the object (javascript calls webservice with the serialized object), it throws an exception because class Forniture is abstract. When I change Forniture class to not being abstract, asp.net convert all the fornitures to Forniture instance instead of the relevand instance (Closet, Table or Chair). How can I serialize/deserialize the inheritance properly so I can solve this problem?
2. Do I use XmlSerialization right?
Thanks!