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
Solved

Writexml overwriting

Posted on 2006-07-20
7
517 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 Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

829 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