using System.Data;
using System.Data.SqlClient;
using System.Xml;
class CommandXmlTest
{
public static void main()
{
string conStr = "Server=localhost;user id=sa;password=wpsjfpxj;database=northwind";
string xquery = "select * from Address FOR XML AUTO, ELEMENT";
SqlConnection conn = new SqlConnection(conStr);
conn.Open();
SqlCommand xcomm = new SqlCommand(xquery, conn);
XmlReader reader = null;
try
{
reader = xcomm.ExecuteXmlReader();
// 노드의 tag와 text만 출력
// 노드가 있다면 true 노드가 없다면 false
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element:
Console.Write("<{0}>", reader.Name);
break;
case XmlNodeType.Text:
Console.Write(reader.Value.Trim());
break;
case XmlNodeType.EndElement:
Console.WriteLine("</{0}>", reader.Name);
break;
}
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
finally
{
reader.Close();
conn.Close();
}
}
}
레퍼런스 : 소설같은 C#
[출처] SqlCommand를 이용한 XmlReader|작성자 맹쥬