Reputation: 9840
I have a simple SQL query, i want to query the database and return the result as XML. I used the key word for XML
, but it says that XML is not a valid syntax.
HELP
using (SqlConnection c= new SqlConnection(cs))
{
string s= "select * from Doctor for XML";
using (SqlCommand cmd = new SqlCommand(s,c))
{
using (SqlDataAdapter ad = new SqlDataAdapter(cmd))
{
ad.Fill(t); // IT SAYS XML IS NOT A VALID SYNTAX
}
}
}
Upvotes: 0
Views: 1603
Reputation: 33809
You can use DataAdapter.Fill()
method to fill query results into a DataTable
and then convert to xml.
Here MSDN link for DataTable.WriteXml() method
using (SqlConnection c= new SqlConnection(cs))
{
string s= "select * from Doctor"; //remove the for xml
using (SqlCommand cmd = new SqlCommand(s,c))
{
using (SqlDataAdapter ad = new SqlDataAdapter(cmd))
{
DataTable dt;
ad.Fill(dt);
//Use DataTable.WriteXml() method
//dt.WriteXml(parameters);
}
}
}
Upvotes: 1
Reputation: 8902
You dont you fill the data into a dataset and convert it into a XML document ? wont that work for you ? Check the following posts
Dataset -> XML Document - Load DataSet into an XML Document - C#.Net
Upvotes: 0