?
Solved

Writexml overwriting

Posted on 2006-07-20
7
Medium Priority
?
531 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
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…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

719 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