Solved

Writexml overwriting

Posted on 2006-07-20
7
514 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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now