Member Avatar for ragnarok511

Hi everyone. I am writing a form that is builds an XML file. The form is multiple pages. Whenever I try to go to to the page after the next page, it tells me that it cannot access the XML file because it is in use by another process. It happens on the doc.load() method.

Here is my code:

protected void next_Click(object sender, EventArgs e)
        {
            XmlDocument doc = new XmlDocument();
                string fileName = (string)Session["FileName"];
              
                    try
                    {
                        doc.Load(fileName);
                    }
                    catch (System.IO.FileNotFoundException)
                    {
                        Server.Transfer("WebForm2.aspx");
                    }
                   
            
            
            XmlElement question = doc.CreateElement("Question");
            XmlAttribute questionattr = doc.CreateAttribute("ID");
            questionattr.Value = questionIndex.ToString();
            question.SetAttributeNode(questionattr);
            
            XmlElement questionText = doc.CreateElement("QuestionText");
            questionText.InnerText = Question.Text;
            question.AppendChild(questionText);
            
            XmlElement answerOne = doc.CreateElement("AnswerOne");
            answerOne.InnerText = ans1.Text;
            question.AppendChild(answerOne);
            
            XmlElement answerTwo = doc.CreateElement("AnswerTwo");
            answerTwo.InnerText = ans2.Text;
            question.AppendChild(answerTwo);
            
            XmlElement answerThree = doc.CreateElement("AnswerThree");
            answerThree.InnerText = ans3.Text;
            question.AppendChild(answerThree);

            XmlElement answerFour = doc.CreateElement("AnswerFour");
            answerFour.InnerText = ans4.Text;
            question.AppendChild(answerFour);

            XmlElement correctAnswer = doc.CreateElement("CorrectAnswer");
            correctAnswer.InnerText = DropDownList1.Text;
            question.AppendChild(correctAnswer);

            doc.DocumentElement.InsertAfter(question,
                                   doc.DocumentElement.LastChild);

            // An instance of FileStream class created
            // The first parameter is the path to the XML file - Catalog.xml

            FileStream fsxml = new FileStream(fileName, FileMode.Truncate,
                                              FileAccess.Write,
                                              FileShare.ReadWrite);

            // XML Document Saved
            doc.Save(fsxml);
            
            
     
        }

Does anyone know a good workaround to this?

Thank You

Recommended Answers

All 2 Replies

do you already try to close your FileStream to release the file handle.

fsxml.Close();
Member Avatar for ragnarok511

Thanks. I was thinking it was the fact I had already loaded it. I closed the file stream using the code above and it worked.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.