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() +
"" + dataColumn.ColumnName + ">");
}
sb.Append(" ");
xmlDocument.LoadXml(sb.ToString());
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() +
"" + dataColumn.ColumnName + ">");
}
sb.Append("
xmlDocument.LoadXml(sb.ToString());
Labels: Loading XML document from a DataTable, Loading XML document from a SqlDataReader