People is the class whose object needs to be converted in XML string which later can be stored in a DB field or as XML file.
public class PersonDetail { public string FirstName { get; set; } public string LastName { get; set; } } public class People { public People() { PeopleList = new List<ProductImage>(); } public List<PersonDetail> PeopleList{ get; set; } }
Function to convert class object to XML string:
public static string SerializeToXml<T>(T value) { StringWriter writer = new StringWriter(CultureInfo.InvariantCulture); XmlSerializer serializer = new XmlSerializer(typeof(T)); serializer.Serialize(writer, value); return writer.ToString(); }
C# code to convert class object to XML string:
People peopleDetailList = new People(); PersonDetail pDetail = new PersonDetail (); pDetail.FirstName= "Vivek"; pDetail.LastName= "Goyal"; peopleDetailList.PeopleList.Add(pDetail); string XMLProductImageList = SerializeToXml(peopleDetailList);
Resultant XML string will be:
<?xml version="1.0" encoding="utf-16"?> <People xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; xmlns:xsd="http://www.w3.org/2001/XMLSchema">; <PeopleList> <PersonDetail> <FirstName>Vivek</FirstName> <LastName>Goyal</LastName> </PersonDetail> </PeopleList> </People>
Comments
Post a Comment