Assume you have the classes like below :
[Serializable]
public class XmlInformation
{
private List<Country> myCountry = new List<Country>();
private List<State> myState = new List<State>();
public List<Country> Country
{
get{ return myCountry; }
set { myCountry = value; }
}
public List<State> State
{
get{ return myState; }
set { myState = value; }
}
}
[Serializable]
public class Country
{
private int myCountryId;
private string myCountryName;
public int CountryID
{
get{ return myCountryId; }
set { myCountryId = value; }
}
public string CountryName
{
get{ return myCountryName; }
set { myCountryName = value; }
}
}
[Serializable]
public class State
{
private int myStateId;
private string myStateName;
public int StateID
{
get{ return myStateId; }
set { myStateId = value; }
}
public string StateName
{
get{ return myStateName; }
set { myStateName = value; }
}
}
You'll be able to export the classes just like the XML fragment in your question using serialization :
XmlInformation xmlClassToExport = new XmlInformation();
using (StreamWriter swExport = new StreamWriter("path\to\filename"))
{
XmlSerializer obj = new XmlSerializer(typeof(XmlInformation));
obj.Serialize(swExport, xmlClassToExport);
SW.Close();
}