using System; using System.Data.SqlClient; using System.Xml; class Program { static void Main(string[] args) { string connectionString = "Data Source=(local);Initial Catalog=MyDatabase;Integrated Security=True"; string queryString = "SELECT * FROM MyTable FOR XML AUTO, XMLDATA"; using (SqlConnection connection = new SqlConnection(connectionString)) { SqlCommand command = new SqlCommand(queryString, connection); connection.Open(); XmlReader reader = command.ExecuteXmlReader(); while (reader.Read()) { Console.WriteLine(reader.ReadOuterXml()); } } } }
using System; using System.Data.SqlClient; using System.Xml; class Program { static void Main(string[] args) { string connectionString = "Data Source=(local);Initial Catalog=MyDatabase;Integrated Security=True"; string queryString = "EXEC MyStoredProcedure @Param1, @Param2 FOR XML AUTO, XMLDATA"; using (SqlConnection connection = new SqlConnection(connectionString)) { SqlCommand command = new SqlCommand(queryString, connection); command.Parameters.AddWithValue("@Param1", "value1"); command.Parameters.AddWithValue("@Param2", "value2"); connection.Open(); XmlReader reader = command.ExecuteXmlReader(); while (reader.Read()) { Console.WriteLine(reader.ReadOuterXml()); } } } }In this example, a stored procedure 'MyStoredProcedure' is executed with two parameters @Param1 and @Param2 and the result is returned as an XML data. The ExecuteXmlReader method returns an XmlReader object that is used to read the XML data returned from the stored procedure. Package library: System.Data.SqlClient.