Click here to Skip to main content
15,891,473 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Dear Developers,
How to transfer data from database to xml file(using xml serialization). I need to transfer the country, state, city and address tables to a single xml file. All are separate tables.
XML
<tblcountry>
<countryid>1</countryid>
<countryname>India</countryname>
</tblcountry>
<tblstate>
<stateid>1</stateid>
<statename>Tamilnadu</statename>
</tblstate>

etc.....
Posted
Updated 4-Apr-11 0:42am
v4

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 :

C#
XmlInformation xmlClassToExport = new XmlInformation();
using (StreamWriter swExport = new StreamWriter("path\to\filename"))
{
  XmlSerializer obj = new XmlSerializer(typeof(XmlInformation));
  obj.Serialize(swExport, xmlClassToExport);
  SW.Close();
}
 
Share this answer
 
If you don't care about the schema, use DataSet.WriteXml (documentation). (Note: The solution referring to DataTable.WriteXml is right for one table, but you have multiple tables here.)

If you do, I think you need to write the save/load code yourself. That is not hard, simply add a <tblXxx> for each table, and then a collection of elements for each row (I suspect you missed this out of your initial snippet) and cell.
 
Share this answer
 
Hi,

You can use System.Xml.Serialization namespace.

Here I am providing a sample code
A a = new A();
XmlSerializer obj = new XmlSerializer(a.GetType());
StreamWriter SW = new StreamWriter("C:\\a.xml");
obj.Serialize(SW, a);
SW.Close();


Where A is a class which contains data.

Regards
Ankit
 
Share this answer
 
One way would be to read the required data into a DataTable and use it's WriteXml[^] method to produce the xml file. Not strictly xml serialization but far simpler IMO.
 
Share this answer
 
Comments
#realJSOP 4-Apr-11 7:16am    
This is an acceptable answer, I don't know why it was down-voted, but I compensated.
Henry Minute 4-Apr-11 7:25am    
Ta very much. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900