Blue Theme Orange Theme Green Theme Red Theme
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Home | Forums | ASP.NET 2.0 Tutorials | Web Services | How Do I...? | Class Browser | WPF Quick Starts | Advertise with Us
 | Consulting  
Submit an Article Submit a Blog 
Search :       Advanced Search »
Home » Blogs Home » Blog Detail

Exporting the data from database to XML using C#

 by nitin singla on Feb 06, 2009

Memory out of exception error while Exporting huge data from database to XML using C#
Comments: 1 Views: 9165 Printable Version 

application there is a utility for exporting the data from database to XML file and then import the same in another system.

 

The system is designed using .NET Framework 3.5.

 

The utility is now failing when trying to export the data around  100000 rows in table. It is giving out of memory exception.

 

The code snippet is as follows

 

 

private void ExportTableAsXml(ref XmlNode rootElement,Hashtable externalParameters)

            {

           

                  DataTable result = null;

                  if (commandTypeField == CommandType.StoredProcedure)

                  {

                  }

                  else if (commandTypeField == CommandType.Text)

                  {

           

                        DataSet resultDataSet =  xdo.ExecDataSetSQL(sqlStatementField);

                        if (resultDataSet != null && resultDataSet.Tables.Count > 0)

                              result = resultDataSet.Tables[0];

                  }

 

 

                  XmlNode containerElement = rootElement;

                  if (result != null && result.Rows.Count != 0)

                  {

 

                        if (containerNameField != string.Empty)

                              containerElement =  rootElement.OwnerDocument.CreateElement(containerNameField);

 

                        if (!ResultIsXml)

                              ConvertTableToXml(ref containerElement, result);

                        else

                        {

                              StringBuilder xmlBuilder = new StringBuilder();

                              if (result.Columns.Count == 1)

                              {

                                    foreach(DataRow row in result.Rows)

                                    {

                                          if (!row.IsNull(result.Columns[0]))

                                          {

                                                xmlBuilder.Append(row[0]);

                                          }

                                    }

                                   

                                    if (xmlBuilder.Length != 0)

                                    {

                                          containerElement.InnerXml += xmlBuilder.ToString();

                        }

                              }

                              else

                              {

                                    throw new Exception("The provided xml query returned more than one column.");

                              }

                        }

 

 

                        if (AllowEmptyElement || (!AllowEmptyElement && containerElement.InnerXml != null && containerElement.InnerXml != string.Empty))

                        {

                              if (rootElement != containerElement)

                                    rootElement.AppendChild(containerElement);

                        }

                  }

 

            }

 

The line highlighted above is throwing exception


Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
 
What do you say about this post? Post a comment here
*Title:
*Comment:
 

Soliton: Exporting the data from database to XML using C# by Bojan Saksida On Jun 14, 2009
This approach uses MEMORY for storing data, you yust hit your OS limitation (Assuming it is 32Bit, 64bit have higher limit). This limitation is for variables not for entire app. Instead use XmlTextWriter, witch dirrectly writes to file and doesnt use any memory for storing data

Team Foundation Server Hosting
Become a Sponsor

 Blogger's Profile
Age: Not Available
Location:
Title: Architect
Joined: Feb 06, 2009
Education: Masters Degree
 More Blogs from this Blogger
Exporting the data from database to XML using C#
View all »
 Latest Blogs
[Video] OnClose Handler
[Video] Storing and Loading the Window and Toolbar position
The Euclidean Algorithm
Swapping Exe Process
How Exe file is Generated by VS2005 C++ Project?
What is Exe
Header files: Multiple Inclusion problem - Solution B
Header files: Multiple Inclusion problem - Solution A
Header files: Multiple Inclusion problem - Reason
Header files: Multiple Inclusion problem
View all »
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 
6 Months Free & No Setup Fees ASP.NET Hosting!
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.