Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8943727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:51:08+00:00 2026-06-15T11:51:08+00:00

My code crashes when using the XmlWriter, saying it is used by another process.

  • 0

My code crashes when using the XmlWriter, saying it is used by another process.

    private void generateXml(Control receivedControl)
    {
        foreach (Control subCtrl in receivedControl.Controls)
        {
            using (XmlWriter writer = XmlWriter.Create("C:\\ui.xml"))
            {
                writer.WriteStartElement(subCtrl.Name);
                generateXml(subCtrl);
                writer.WriteEndElement();
            }
        }
    }

    private void button2_Click(object sender, EventArgs e)
    {
        foreach (Control c in this.Controls)
        {
            generateXml(c);
        }
    }

Here’s the error I get debugging:

Additional information: The process cannot access the file ‘C:\ui.xml’ because it is being used by another process.

EDIT: So thanks to you I’ve managed to get the recursion working, but it only writes the last object in the xml file, anybody knows why? Here’s the updated code:

    private void generateXml(XmlWriter receivedWriter, Control receivedControl)
    {
        receivedWriter.WriteStartElement(receivedControl.Name);
        foreach (Control subCtrl in receivedControl.Controls)
        {
            generateXml(receivedWriter, subCtrl);
        }
        receivedWriter.WriteEndElement();
    }
    private void button2_Click(object sender, EventArgs e)
    {
        foreach (Control c in this.Controls)
        {
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;

            using(XmlWriter writer = XmlWriter.Create("c:\\ui.xml", settings))
            {
                writer.WriteStartDocument();
                writer.WriteStartElement("Form");
                generateXml(writer, c);
                writer.WriteEndElement();
                writer.WriteEndDocument();
            }
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T11:51:09+00:00Added an answer on June 15, 2026 at 11:51 am

    The file when accessed by the XmlWriter will become locked, so an subsequent attempts to read it result in error.

    You can change the function to pass the writer as a parameter, thereby have one copy and hopefully avoid the issue.

    private void generateXml(XmlWriter writer, Control receivedControl)
    {
        foreach (Control subCtrl in receivedControl.Controls)
        {
                writer.WriteStartElement(subCtrl.Name);
                generateXml(writer, subCtrl);
                writer.WriteEndElement();
        }
    }
    
    private void button2_Click(object sender, EventArgs e)
    {
        using (XmlWriter writer = XmlWriter.Create("C:\\ui.xml"))
        {
            writer.WriteStartDocument();
            writer.WriteStartElement(this.Name); // This is the document element
            foreach (Control c in this.Controls)
            {
                generateXml(writer, c);
            }
            writer.WriteEndDocument(); // Close any open tags
        }
    }
    

    Above is a sample – not tested.

    Edit : Updated to include root element

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using following code. When the query crashes, it is not displaying the
The following code crashes while sorting the vector. #include<cstdio> #include<vector> #include<algorithm> using namespace std;
my code broke somewhere along the way, and crashes when using the navigation bar
I'm using nasm to compile the following assembly. However the code crashes in the
The following code crashes and burns and I don't understand why: DateTime dt =
Every now and then (ahem...) my code crashes on some system; quite often, my
I wanted to know why the following code crashes. int main( ) { int
Beginner in Android development. My code crashes. I have made a simple Java method
I've been struggling with this for a while now, and this code crashes with,
When I run the following Code it crashes at the last line. I don't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.