.NET: Best way to write text files

Hi, I am writing data from a database to a text file, actually an xml file, to which I apply my own formatting in order to transform it in a pdf. I am using iTextCSharp utility. Here is my proceeding:  I load all the data from the database to the server memory and from there I write the data chunck by chunk to a xml file on the local hard drive (using a TextWriter) and once done, I transform that file into xml. My question is, since I don't gain so much in performance (I just was looking for a way of avoiding memory overrun), is there a better way of getting the same result by the same time gaining performance?
LVL 4
karakavAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

psdavisCommented:
If you read the database into a DataSet instead, then you can just use the DataSet's WriteXml method to create the final file directly.
0
karakavAuthor Commented:
I don´t want to generate the xml file at once because that's normally where the memory overrun occurs. Unless the DataSet's WriteXml method behave differently.
0
Tony McCreathTechnical SEO ConsultantCommented:
It sounds like you have a large set of data.

Idealy, you would want to load from your database using a DataReader and process the data into an XmlTextWriter to your file. This would be the fastest and would use minimum memory
0
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

psdavisCommented:
Tiggerito is correct.  If your database is huge, then the DataReader is your best option.
0
karakavAuthor Commented:
Actually there is another detail that I forgot to mention: I have an xml file that contains the formatting I want for all my reports. Basing on that file, I place rows that I get from the database in specific places in that xml file (using a StringBuilder) and then from there I generate the final xml file to be parsed into a pdf document. Do you think the XmlTextWriter can give that flexibility?
0
Tony McCreathTechnical SEO ConsultantCommented:
I don't quite get what you are doing.

Is this xml file basically wrapper xml that you want to insert your generated xml into, and store the result into a new file.? like a template?

Or is it more complex?
<Report>
  <ReportHeader>Bla</ReportHeader>
  <ReportContent>
    <!-- database data goes here -->
  </ReportContent>
</Report>

Open in new window

0
karakavAuthor Commented:
No it is a more complex xml file that needs to be treated part by part, even if the elements and attributes may be similar. For instance, iTextSharp define an element that is proper to it that is called <newpage> and which defines where a new page starts. So I need to insert that element at particular places when inserting data row by row.
0
Tony McCreathTechnical SEO ConsultantCommented:
If you can serial process your xml template file then you could read it using an XmlReader and output it to a file using an XmlWriter.

When you reach points where you need to insert database data then you could use the proposed process to read the data using a DataReader and write it to the same XmlWriter as above.


0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
karakavAuthor Commented:
Thanks.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.