Skip to content

Instantly share code, notes, and snippets.

@pedroinfo
Last active November 4, 2016 17:41
Show Gist options
  • Save pedroinfo/6919155681214e607186beb2e695774d to your computer and use it in GitHub Desktop.
Save pedroinfo/6919155681214e607186beb2e695774d to your computer and use it in GitHub Desktop.
DataSet To Xml (Convert Multiple Tables to XML file)
string connectionString = "Server=192.168.2.56;Database=NEWDB;User Id=SA;Password=123456;";
var conn = new SqlConnection(connectionString);
conn.Open();
var tables = new[] { "Categories", "CustomerCustomerDemo", "Employees", "Territories" };
var sb = new StringBuilder();
foreach (var table in tables)
{
var ds = new DataSet();
var query = "SELECT * FROM " + table + " ";
var cmd = new SqlCommand(query, conn);
var da = new SqlDataAdapter(cmd);
da.Fill(ds, table);
sb.AppendLine(ds.GetXml());
}
File.WriteAllText(@"c:\\temp\arquivo.xml", "<root>"+ sb.ToString() + "</root>");
conn.Close();
conn.Dispose();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment