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 6854901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:41:28+00:00 2026-05-27T01:41:28+00:00

Hoping I might be able to get some help with an infuriating problem which

  • 0

Hoping I might be able to get some help with an infuriating problem which I can’t seem to find a definitive answer for anywhere.

I am appending data to an xml document using an XDocument object, and then overwriting the existing file using xDoc.save(path), but for some reason the first time I run the code an error is thrown even though the file is not in use by any other process.

*”The process cannot access the file “C:\XXX\XXXX\Telemetry\2011_11_22.tlm because it is being used by another process.”*

Subsequent iterations do not cause a problem.

Here is my code that I am using with try/catch removed for readability:-

XElement x = GenerateTelemetryNode(h); //Create a new element to append

            if (File.Exists(path))
            {
                if (xDoc == null)
                {
                    xDoc = XDocument.Load(new StreamReader(path));
                }
            }
            else
            {
                xDoc = new XDocument();
                xDoc.Add(new XElement("TSD"));
            }
            xDoc.Element("TSD").Add(x);
            xmlPath = path;
            xDoc.Save(path);

I’m sure there’s a very simple explanation for it.

Many thanks in advance for any replies.

  • 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-05-27T01:41:29+00:00Added an answer on May 27, 2026 at 1:41 am

    I would expect the problem is the StreamReader hasn’t been disposed in which case it will still be attached to the document. I would suggest using wrapping the StreamReader creation in a using clause to ensure that is disposed of immediately after the document has been loaded:

     if (xDoc == null)
     {
            using (var sr = new StreamReader(path))
            {        
               xDoc = XDocument.Load(new StreamReader(sr));
            }
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having some strange drawing artefacts that I'm hoping someone might be able to
Hoping I can get some architectural advice on this. I have been given the
I'm getting some serious weirdness using FileVersionInfo.GetVersionInfo() and was hoping somebody might be able
I was hoping somebody might be able to give me a hand by pointing
I was hoping someone might help with a function that given two parameters: @param
I'm concerned that this might be working on an NP-Complete problem. I'm hoping someone
Hoping someone can provide an answer with this, although it's not 100% programming related.
Hoping someone can help me with a slight hurdle I've come up against in
Hoping some learned Rails developers here can recommend an existing Ruby on Rails plugin
Hoping someone can shed some light on this: Do lookup tables need their own

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.