// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.IO; using System.Xml; using System.Text; using System.Data; using System.Data.SqlClient; class MainClass { public static void DataTableWrite(DataTable dt, TextWriter tw) { XmlTextWriter xtw = new XmlTextWriter(tw); xtw.Formatting = Formatting.Indented; xtw.WriteStartDocument(); xtw.WriteStartElement("NewDataTable"); for(int row = 0; row < dt.Rows.Count; row++) { xtw.WriteStartElement(dt.TableName); for(int col = 0; col < dt.Columns.Count; col++) { xtw.WriteStartElement(dt.Columns[col].ColumnName); xtw.WriteString(dt.Rows[row].ItemArray[col].ToString()); xtw.WriteEndElement(); } xtw.WriteEndElement(); } xtw.WriteEndElement(); xtw.WriteEndDocument(); xtw.Close(); tw.WriteLine(); } public static void Main(string[] args) { // vi fylder et data set med data SqlConnection con = new SqlConnection("server=ARNEPC2\\ARNEPC2RUN;Integrated Security=SSPI;database=TestMSDE"); con.Open(); SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM T1", con); DataSet ds = new DataSet(); da.Fill(ds, "T1"); // udskrivning af hele data set ds.WriteXml(Console.Out); Console.WriteLine(); // special udskrivning af tabel DataTableWrite(ds.Tables["T1"], Console.Out); // færdig con.Close(); } }