Solved

Writexml overwriting

Posted on 2006-07-20
7
519 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: 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!

 

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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

734 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