.Net Links

Tuesday, January 4, 2011

Loading XML document from a SqlDataReader

SqlConnection sqlconn = new SqlConnection(@"Data Source=ABC;Initial Catalog=ABC;Integrated Security=SSPI;Trusted_Connection=True");
SqlCommand command = new SqlCommand("SPName", sqlconn);
command.CommandType = System.Data.CommandType.StoredProcedure;
command.CommandText = "SPName";
command.Connection = sqlconn;
sqlconn.Open();

SqlDataReader dr = command.ExecuteReader();

DataTable dt = new DataTable();
dt.Load(dr);

XmlDocument xmlDocument = new XmlDocument();
StringBuilder sb = new StringBuilder();

sb.Append("");
sb.Append("");

foreach (DataRow dataRow in dt.Rows)
{
foreach (DataColumn dataColumn in dt.Columns)
sb.Append("<" + dataColumn.ColumnName + ">" +
dataRow[dataColumn].ToString() +
"");


}

sb.Append("
");
xmlDocument.LoadXml(sb.ToString());

Labels: ,

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]



<< Home