Click here to Skip to main content
15,920,111 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I need to generate XML from a SQL table.

My xml need to be like this.

XML
<?xml version="1.0" encoding="utf-8" ?>
<graph caption='Sample xml' xAxisName='Country' 
     yAxisName='GDP' showNames='1' decimalPrecision='0' formatNumberScale='0'>
  <set name='Qatar' value='102768' color='#FF0000' />
  <set name='Luxembourg' value='80679' color='#00FF00' />
  <set name='Singapore' value='60883' color='#0000FF' />
  <set name='Norway' value='55264' color='#00FFFF' />
  <set name='Brunei' value='50526' color='#FF00FF' />
  <set name='United States' value='49802' color='#FFFF00' />
  <set name='UAE' value='48992' color='#FFAAFF' />
  <set name='Switzerland' value='45285' color='#CCFFBB' />
  <set name='Kuwait' value='43846' color='#00AABB' />
  <set name='Austria' value='42477' color='#BBCCFF' />
</graph>


My sql table structure will be like this

SQL
Name	    Value       Color
----         -----       ------
Qatar	    102768      #FF0000
Luxembourg   80679	#00FF00
Singapore    60883	#0000FF
Norway	    55264	#00FFFF


My code behind will be like this..

C#
protected void Button1_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection("Data Source=.; uid=sa; pwd=wintellect;database=Rohatash;");
            string strSQL = "Select name, value, color from Details";
            SqlDataAdapter dt = new SqlDataAdapter(strSQL, con);
            DataSet ds = new DataSet();
            dt.Fill (ds,"Details");
            ds.WriteXml(Server.MapPath("Sample_xml_file.xml"));
        }     


This code is not giving me deisred result.

Please help to get the desired XML output.
Posted

Use LINQ to do that.Here is an example:
Create xml from database using LINQ[^]
 
Share this answer
 
 
Share this answer
 

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