Convert the result from a query to XML and output (Disconnected mode) : ResultSet to Xml « ADO.Net « C# / CSharp Tutorial






using System;
using System.Xml;
using System.Data;
using System.Data.SqlClient;

class MainClass {
    public static void Main(string[] args)
    {
        XmlDocument doc = new XmlDocument();

        using (SqlConnection con = new SqlConnection())
        {
            con.ConnectionString = "server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI;";

            SqlCommand com = con.CreateCommand();
            com.CommandType = CommandType.Text;
            com.CommandText = "SELECT ID, FirstName FROM Employee FOR XML AUTO";

            con.Open();

            XmlReader reader = com.ExecuteXmlReader();
            doc.LoadXml("<results></results>");

            XmlNode newNode = doc.ReadNode(reader);

            while (newNode != null)
            {
                doc.DocumentElement.AppendChild(newNode);
                newNode = doc.ReadNode(reader);
            }
        }

        Console.WriteLine(doc.OuterXml);
    }
}








32.51.ResultSet to Xml
32.51.1.SqlCommand.ExecuteXmlReader
32.51.2.Convert the result from a query to XML and output
32.51.3.Convert the result from a query to XML and output (Disconnected mode)
32.51.4.Save data stored in table to xml file