Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Writexml overwriting

Posted on 2006-07-20
7
Medium Priority
?
543 Views
Last Modified: 2008-02-01
I'm sure that this is probably something simple but I just can't see it. I'm using a streamwriter to read lines of text from a text file, populate a dataset then export to an xml file using writexml but every time I go through the loop it writes over the text in the file instead of adding on... I tried moving the writexml 2 brackets down but then I get more errors... can anyone see what's wrong?
 
foreach(string F in fileName)
                {
                    File.Copy(F,backupDir+DateTime.Now.ToString("yyyyMMdd-hhmmss")+"_"+Path.GetFileName(F));  
                    // to take policydataset and parse out info
                    //StreamWriter sw = new StreamWriter(fileResultName);
                    //sw.WriteLine("<?xml version='1.0' encoding='ISO-8859-1'?>\n<memberdata>");
                  using (StreamReader sr = new StreamReader(F))
                  {
                        string line;
                       
                        // Read and display lines from the file until the end of
                        // the file is reached.
                        while ((line = sr.ReadLine()) != null)
                        {
                              pgbStatus.PerformStep();
                              IsisSetPolicyDataSetDefaults();
                              polDS.Policy[0].CLIENT = "CAA";
                              polDS.Policy[0].DIVISION = line.Substring(3,3);
                              polDS.Policy[0].BRANCH = line.Substring(3,5);
                              // i took a bunch of stuff out of here to shorten it

                              string justFileName = Path.GetFileNameWithoutExtension(F);
                              polDS.WriteXml(fileResultDir+justFileName+".xml");
                                          

                        }
                  }
                            }
0
Comment
Question by:boukaka
  • 3
  • 3
7 Comments
 
LVL 29

Expert Comment

by:Nightman
ID: 17149379
Try creating a streamwriter (or text writer) for the file to which you would like to append, and then pass this object in to the WriteXml method (I think that it has a number of overloads). If I remember correctly, there should be an example of this in the DataSet.WriteXml Method documentation in the online help for visualk studio.

Or course, you could always build up a complete dataset and then write the entire contents to xml with the method that you are currently using .

Night
0
 

Author Comment

by:boukaka
ID: 17151006
What do you mean by build up a complete dataset..?? How is that done? Would that work with files that will be all different sizes (i.e. some will have 10 lines, some 900).

I was hoping that there would be a way to "append" to the Writexml.. something like WriteXml.Append(... but I guess I'm dreaming.

Also, can you give me an example of how I would append to a streamwriter??? I'm pretty new to c# and I'm muddling along here.

Thanks!
0
 
LVL 10

Expert Comment

by:NetworkArchitek
ID: 17151068
Just put it in a StringReader then do an append with the ToString() method of the StringReader object which has your XML.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:boukaka
ID: 17151092
Ok, now I'm really lost...I'm sorry, you're assuming that I understand more than I do about this stuff..


StringReader sr = new StringReader(fileResultDir+justFileName+".txt")... this reads the dataset?? I would put this before the dataset??
then how would I append to it...
after I get it all appended, how would I get it to the WriteXml field..

like this? sr.WriteXml(... file name here)

0
 
LVL 10

Accepted Solution

by:
NetworkArchitek earned 1600 total points
ID: 17151134
Sorry, StringWriter.

StringWriter stringWriter = new StringWriter();

ds.WriteXml(stringWriter);

// Now with your StreamWriter sw
sw.Append(stringWriter.ToString());
0
 

Author Comment

by:boukaka
ID: 17151141
AHHHHHH!!! Now I get it Thanks!
0
 
LVL 10

Expert Comment

by:NetworkArchitek
ID: 17151244
No problem, glad to be of help. =)
0

Featured Post

Industry Leaders: 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!

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month10 days, 7 hours left to enroll

886 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