I need to create a XML files from a dataset or datable; foreach record in dataset I have to create one file, following my code:
SqlDataAdapter da = new SqlDataAdapter("select top 5 * from dbo.Log", con);
DataSet ds = new DataSet();
da.Fill(ds, "Log");
DataTable dt = new DataTable("Log");
da.Fill(dt);
foreach (DataRow row in dt.Rows)
{
row.Table.WriteXml("D:\\" + row["id"] + ".xml");
// some other code...
That create 5 XML files but inside there are all 5 records found in the dataset/datatable... I want only one record for every file! How can I do so? thanks
You're using
row.Table.WriteXml(...). This writes the entire table, so: all rows. As far as I know, there is no inbuilt API for exporting a single row at a time; I guess you could clone/add/remove the row into an identical but empty table?Something like (untested):
It might be easier to use
XmlWriter, though.