0

I am populating an xml document using c#. If I populate it once then everything is ok but when I try to repopulate it a second time (without closing the program) I get an error message and rubish gets written to the bottom of the xml file.

I'm sure I've found the answer to this question before but I can't find it.

I'm sure its something to do with the fact that I'm not closing something down after updating the xml document or something but I can't remember what exactly I have to close down.

Sorry I hope you all understand. I find it difficult to explain.

Code:

using (FileStream READER = new FileStream(fpath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
      {
                System.Xml.XmlDocument Template = new System.Xml.XmlDocument();// Set up the XmlDocument //
                Template.Load(READER); //Load the data from the file into the XmlDocument //

                //**********Grab nodes to be written to********


                using (FileStream WRITER = new FileStream(fpath, FileMode.Open, FileAccess.Write, FileShare.ReadWrite))
                {
                    //Set up the filestream (READER) //
                    //Write the data to the filestream
                    Template.Save(WRITER);
5
  • 3
    It'll be easier to understand if you include the error message and maybe some code, if possible. Commented Apr 12, 2011 at 15:20
  • the reason i get the error message is because the xml is not correct because of the rubbish that gets added to the end when writing to the xml file the second time. Once i delete that again then things are fine. Commented Apr 12, 2011 at 15:27
  • Could it be that you are inserting nodes in the wrong spot? For example, there should be only one root node, but if you open the file and add more contents to it at the bottom and don't insert it within the root node you've got a malformed xml document Commented Apr 12, 2011 at 15:30
  • im sure i have to close the reader or writer or something but i cant remember and everything i try fails. Commented Apr 12, 2011 at 15:30
  • @prescott no they are correct. They work fine first of all then if i click the update button again they dont. I've sorted it out before but I accidentally lost the code...now I'm struggling to remember it Commented Apr 12, 2011 at 15:31

1 Answer 1

1

You read and write in the same file fpath

You have to close the reader after Template.Load(READER);

  using (FileStream READER = new FileStream(fpath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
  {
       System.Xml.XmlDocument Template = new System.Xml.XmlDocument();// Set up the XmlDocument //
       Template.Load(READER); //Load the data from the file into the XmlDocument //
  }

  //**********Grab nodes to be written to********


  using (FileStream WRITER = new FileStream(fpath, FileMode.Open, FileAccess.Write, FileShare.ReadWrite))
  {
     //Set up the filestream (READER) //
     //Write the data to the filestream
     Template.Save(WRITER);
     ...
  }
Sign up to request clarification or add additional context in comments.

1 Comment

Just close the using(FileStream READER after Template.Load(READER); like the code in my answer (or after that if you still need READER) but before opening the WRITER.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.