0

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

Edited by ragnarok511: Forgot to add a comment

2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by ragnarok511
0

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

fsxml.Close();
0

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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.