Saturday, June 11, 2011

Multi level xml generation using XMLSerialization in C#.Net


//In memory XML generation using Serialization//
XmlSerializer srl = new XmlSerializer(typeof(UserLogin));
StringWriter sw = new StringWriter();
srl.Serialize(sw, oUserLogin);
string xml = sw.GetStringBuilder().ToString();


[Serializable]
[XmlRoot("Users")]
public class UserDetails
{
//First way to create inner XML nodes
[XmlArray("AddressDetails")]
public List AddressDetail { get; set; }

//Second way to create inner XML nodes
[XmlArray("ContactDetails")]
[XmlArrayItem("ContactDetail")]
public List ContactDetail { get; set; }
}


[Serializable]
[XmlType("AddressesDetail")]
public class AddressDetails
{
public string ........ (Properties)
}

[Serializable]
public class ContactDetails
{
public string ........ (Properties)
}