Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Serialize SqlDataReader result to XML
Message
 
 
To
All
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Title:
Serialize SqlDataReader result to XML
Environment versions
Environment:
C# 4.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Miscellaneous
Thread ID:
01562506
Message ID:
01562506
Views:
51
Hi everybody,

I am reading some code
// 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();
I am thinking there may be a way to write the above more efficiently with a fewer lines of code.

Do you think it's possible or the above is as good as it gets?

Thanks in advance.
If it's not broken, fix it until it is.


My Blog
Next
Reply
Map
View

Click here to load this message in the networking platform