Basically you mean: Converts Object to XML & Converts XML to Object. This can be performed by
using System.Xml.Serialization;
Lets just say you have a list as :
public List<Product> GetProductList()
{
List<Product> list = new List<Product>();
Product product = new Product(new Section[3]);
product.Sections[0] = new Section("1", new Header[3]);
product.Sections[0].Headers[0] = new Header("P1", "C1", "T1");
product.Sections[0].Headers[1] = new Header("P2", "C2", "T2");
product.Sections[1] = new Section("2", new Header[3]);
product.Sections[1].Headers[0] = new Header("P1", "C1", "T1");
product.Sections[1].Headers[1] = new Header("P2", "C2", "T2");
list.Add(product);
return list;
}
You can serialize it as :
private void Serialize()
{
List<Product> list = GetProductList();
XmlSerializer serializer = new XmlSerializer(typeof(List<Product>));
TextWriter tw = new StreamWriter(Server.MapPath("book1.xml"));
serializer.Serialize(tw, list);
tw.Close();
}
And deserialize the same as :
public void DeSerialize()
{
XmlSerializer serializer = new XmlSerializer(typeof(List<Product>));
TextReader tr = new StreamReader(Server.MapPath("book1.xml"));
List<Product> b = (List<Product>)serializer.Deserialize(tr);
tr.Close();
Foreach (Product product in b)
{
Response.Write(product.Sections[0].Name + ",");
Response.Write(product.Sections[1].Name);
}
}