Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Serialize SqlDataReader result to XML
Message
 
 
À
Tous
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Titre:
Serialize SqlDataReader result to XML
Versions des environnements
Environment:
C# 4.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Divers
Thread ID:
01562506
Message ID:
01562506
Vues:
50
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
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform