// Arquivo de exemplo: ExecuteXmlReader.cs
// Para compilar utilize: csc ExecuteXmlReader.cs
using System;
using System.Data;
using System.Data.SqlClient;
using System.Xml;

public class Programa {
	public static void Main() {
		string conectar = @"Data Source=.\SQLExpress;Integrated Security=SSPI;Initial Catalog=Northwind;";
		string sSql = "select Products.ProductName, Products.QuantityPerUnit,Products.UnitPrice from Products FOR XML AUTO";
		XmlReader r = null;
		try
		{
			using (SqlConnection conn = new SqlConnection(conectar)) {
				SqlCommand cmd = new SqlCommand(sSql, conn);
				conn.Open();
				r = cmd.ExecuteXmlReader();
				Console.WriteLine("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>");
				Console.WriteLine("<Produtos>");
				while (r.Read()) {
					Console.WriteLine("\t" + r.ReadOuterXml());
				}
				Console.WriteLine("</Produtos>");	
				Console.Read();
			
			}
		}
		catch (SqlException)
		{
			Console.Write("Erro SQL.");
		}
		catch (Exception)
		{
			Console.Write("Ocorreu um erro.");
		}
		finally
		{	
			if(r!=null)r.Close();	
		}		
	}
}
