[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

.NET: Best way to write text files

Posted on 2008-11-17
9
Medium Priority
?
432 Views
Last Modified: 2013-12-17
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?
0
Comment
Question by:karakav
  • 4
  • 3
  • 2
9 Comments
 
LVL 7

Expert Comment

by:psdavis
ID: 22977581
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
 
LVL 4

Author Comment

by:karakav
ID: 22982673
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
 
LVL 23

Expert Comment

by:Tony McCreath
ID: 22983949
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Expert Comment

by:psdavis
ID: 22984155
Tiggerito is correct.  If your database is huge, then the DataReader is your best option.
0
 
LVL 4

Author Comment

by:karakav
ID: 22999122
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
 
LVL 23

Expert Comment

by:Tony McCreath
ID: 23000730
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
 
LVL 4

Author Comment

by:karakav
ID: 23011019
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
 
LVL 23

Accepted Solution

by:
Tony McCreath earned 2000 total points
ID: 23012699
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
 
LVL 4

Author Closing Comment

by:karakav
ID: 31517508
Thanks.
0

Featured Post

Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question