Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Generate an xml using c# and sql server

Status
Not open for further replies.

Raul2005

Programmer
Sep 23, 2005
44
0
0
US
Need some help to Generate an XML file from SQL server by using a sql statement or store procedure with c#.

Thanks

 
do you mean you need the results from an sql statement thrown into an xml file? If so then read on, if not, then please elborate.

Code:
SqlConnection objConn = new SqlConnection("connString");
SqlDataAdapter	objDA = new SqlDataAdapter("sql command",objConn);
DataSet ds = new DataSet();
objDA.Fill(ds);
string xml = ds.GetXml();

the variable xml now holds the results from your sql command in an xml format, you can save it to a file or do what ever you need to do with it.
 
You can also use the "For XML" command in SQL Server to return XML right from the database. See this link for more information.



But, if you want to do it in your .Net code, ralphtrent has you all set up.

----------------------------------------

TWljcm8kb2Z0J3MgIzEgRmFuIQ==
 
I create a C# console application and this code provide me what I want, but I would like to
create from the result I obtained an xml file store at the c drive. An additional require is to apply a
dtd file for validation purpose. Could some one help me to complete this code.

-------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace XMLBond_Hub
{
class Program
{
static void Main()
{

SqlConnection connString = new SqlConnection("Server=test1;UID=usuario;PWD=pass;Database=xxx");
SqlDataAdapter objDA = new SqlDataAdapter("select First_Name,Last_Name,Email,Company_Name from contact ", connString);
DataSet ds = new DataSet();
objDA.Fill(ds);
string xml = ds.GetXml();

}
}
}

Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top