I give you a general solution
Create class **
public class Person
{
public string Email { get; set; }
public string DOB { get; set; }
public string City { get; set; }
}
**
After that write this method in any class library like this
Public Class Utilities
{
public static XmlElement Serialize(object transformObject)
{
XmlElement serializedElement = null;
try
{
MemoryStream memStream = new MemoryStream();
XmlSerializer serializer = new XmlSerializer(transformObject.GetType());
serializer.Serialize(memStream, transformObject);
memStream.Position = 0;
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(memStream);
serializedElement = xmlDoc.DocumentElement;
}
catch (Exception SerializeException)
{
}
return serializedElement;
}
}
Now write this main function on your page where you want to perform this task.
private void MainMethod()
{
Collection<Person> mPersons = new Collection<Person>();
Person sPerson = new Person();
sPerson.City = "City 1";
sPerson.DOB = DateTime.Now.ToString("YYYY-MM-DD HH:MM:SS");
sPerson.Email = "email_1@email.com";
mPersons.Add(sPerson);
sPerson = new Person();
sPerson.City = "City 2";
sPerson.DOB = DateTime.Now.ToString("YYYY-MM-DD HH:MM:SS");
sPerson.Email = "email_2@email.com";
mPersons.Add(sPerson);
XmlElement xE = (XmlElement)Utilities.Serialize(mPersons);
XmlDocument xDoc = new XmlDocument();
xDoc.LoadXml(xE.OuterXml.ToString());
xDoc.Save(Server.MapPath("myFile.xml"));
}
Try this if you have a class object or dataset
source
share