>>>// With SqlDataReader, you don't know how many records their are unless you count them. >>> recordCount = 0; >>> try >>> { >>> // StringBuilder for the entire return string and one for the records >>> StringBuilder returnValue = new StringBuilder(); >>> StringBuilder recordValues = new StringBuilder(); >>> >>> // Add the type and the Table start tags >>> returnValue.Append("<XML>"); >>> returnValue.Append("<Table>"); >>> >>> // Loop through the records and get the column names, values, and record count >>> while (sqlDataReader.Read()) >>> { >>> // There is no sqlDataReader.RecordCount property - we need to increment a counter to figure it out >>> recordCount++; >>> >>> // Add the Record tag >>> recordValues.Append("<Record>"); >>> >>> // Loop through the columns and get the name and value >>> for (Int32 i = 0; i < sqlDataReader.FieldCount; i++) >>> { >>> // Store the appropriate format of the column name based on return type >>> String columnName = sqlDataReader.GetName(i); >>> >>> // Get the column value based on the column type >>> String columnValue = GetColumnValue(sqlDataReader, i, true); >>> >>> // Set the string for the record in the appropriate format (removing any empty values from the return) >>> recordValues.AppendFormat("<{0}>{1}</{0}>", columnName, columnValue); >>> } >>> >>> // The end Record tag >>> recordValues.Append("</Record>"); >>> } >>> >>> // Add the record count for the XML return type >>> returnValue.AppendFormat("<{0}>{1}</{0}>", "RecCount", recordCount); >>> >>> // Append all of the record values (already formatted) >>> returnValue.Append(recordValues); >>> returnValue.Append("</Table>"); >>> returnValue.Append("</XML>"); >>> >>> // Return the whole string... >>> return returnValue.ToString();>>>