Link to home
Start Free TrialLog in
Avatar of karakav
karakav

asked on

.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?
Avatar of psdavis
psdavis
Flag of United States of America image

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.
Avatar of karakav
karakav

ASKER

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.
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
Tiggerito is correct.  If your database is huge, then the DataReader is your best option.
Avatar of karakav

ASKER

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?
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

Avatar of karakav

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of Tony McCreath
Tony McCreath
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of karakav

ASKER

Thanks.