I have a scenario where i need to have xml data from list of objects to send data in a generalized format through http.
This is a generic function by which we can convert a single object as well as a list of objects into xml, object can have list of child objects also.
Main Function :
public string ConvertObjectToXML<T>(T obj)
{
System.Xml.Serialization.XmlSerializer xsSubmit = new System.Xml.Serialization.XmlSerializer(typeof(T));
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
System.IO.StringWriter sww = new System.IO.StringWriter();
System.Xml.XmlWriter writer = System.Xml.XmlWriter.Create(sww);
xsSubmit.Serialize(writer, obj);
var xml = sww.ToString(); // Your xml context.Response.Write(xml);
return xml;
}
Implementation Example :
Test Objects:
public class Order
{
public string orderno { get; set; }
public List<OrderDetails> lst { get; set; }
}
public class OrderDetails
{
public string itemname { get; set; }
public string itemqty { get; set; }
}
Execution to get result :
public string About()
{
OrderDetails orderDetail1 = new OrderDetails { itemname="s", itemqty="1" };
OrderDetails orderDetail2 = new OrderDetails { itemname = "s2", itemqty = "2" };
OrderDetails orderDetail3 = new OrderDetails { itemname = "s3", itemqty = "3" };
List<OrderDetails> lstOrderDetails1 = new List<OrderDetails>();
lstOrderDetails1.Add(orderDetail1);
lstOrderDetails1.Add(orderDetail2);
lstOrderDetails1.Add(orderDetail3);
Order order1 = new Order();
order1.orderno = "001";
order1.lst = lstOrderDetails1;
OrderDetails orderDetail4 = new OrderDetails { itemname = "s4", itemqty = "4" };
OrderDetails orderDetail5 = new OrderDetails { itemname = "s5", itemqty = "5" };
OrderDetails orderDetail6 = new OrderDetails { itemname = "s6", itemqty = "6" };
List<OrderDetails> lstOrderDetails2 = new List<OrderDetails>();
lstOrderDetails2.Add(orderDetail4);
lstOrderDetails2.Add(orderDetail5);
lstOrderDetails2.Add(orderDetail6);
Order order2 = new Order();
order2.orderno = "002";
order2.lst = lstOrderDetails2;
List<Order> lstorder = new List<Order>();
lstorder.Add(order1);
lstorder.Add(order2);
string resultXml = ConvertObjectToXML<List<Order>>(lstorder);
return resultXml;
}
This is a generic function by which we can convert a single object as well as a list of objects into xml, object can have list of child objects also.
Main Function :
public string ConvertObjectToXML<T>(T obj)
{
System.Xml.Serialization.XmlSerializer xsSubmit = new System.Xml.Serialization.XmlSerializer(typeof(T));
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
System.IO.StringWriter sww = new System.IO.StringWriter();
System.Xml.XmlWriter writer = System.Xml.XmlWriter.Create(sww);
xsSubmit.Serialize(writer, obj);
var xml = sww.ToString(); // Your xml context.Response.Write(xml);
return xml;
}
Implementation Example :
Test Objects:
public class Order
{
public string orderno { get; set; }
public List<OrderDetails> lst { get; set; }
}
public class OrderDetails
{
public string itemname { get; set; }
public string itemqty { get; set; }
}
Execution to get result :
public string About()
{
OrderDetails orderDetail1 = new OrderDetails { itemname="s", itemqty="1" };
OrderDetails orderDetail2 = new OrderDetails { itemname = "s2", itemqty = "2" };
OrderDetails orderDetail3 = new OrderDetails { itemname = "s3", itemqty = "3" };
List<OrderDetails> lstOrderDetails1 = new List<OrderDetails>();
lstOrderDetails1.Add(orderDetail1);
lstOrderDetails1.Add(orderDetail2);
lstOrderDetails1.Add(orderDetail3);
Order order1 = new Order();
order1.orderno = "001";
order1.lst = lstOrderDetails1;
OrderDetails orderDetail4 = new OrderDetails { itemname = "s4", itemqty = "4" };
OrderDetails orderDetail5 = new OrderDetails { itemname = "s5", itemqty = "5" };
OrderDetails orderDetail6 = new OrderDetails { itemname = "s6", itemqty = "6" };
List<OrderDetails> lstOrderDetails2 = new List<OrderDetails>();
lstOrderDetails2.Add(orderDetail4);
lstOrderDetails2.Add(orderDetail5);
lstOrderDetails2.Add(orderDetail6);
Order order2 = new Order();
order2.orderno = "002";
order2.lst = lstOrderDetails2;
List<Order> lstorder = new List<Order>();
lstorder.Add(order1);
lstorder.Add(order2);
string resultXml = ConvertObjectToXML<List<Order>>(lstorder);
return resultXml;
}
No comments:
Post a Comment