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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:07:40+00:00 2026-06-17T08:07:40+00:00

We have a website application which acts as a form designer. The form data

  • 0

We have a website application which acts as a form designer.

The form data is stored in XML file. Each form has it’s own xml file.

When i edit a form, i basically recreate the XML file.

public void Save(Guid form_Id, IEnumerable<FormData> formData)
{
    XDocument doc = new XDocument();
    XElement formsDataElement = new XElement("FormsData");
    doc.Add(formsDataElement);
    foreach (FormData data in formData)
    {
        formsDataElement.Add(new XElement("FormData",
                                 new XAttribute("Id", data.Id)
                                 new XAttribute("Name", data.Name)
                                 // other attributes
                             ));
    }

    doc.Save(formXMLFilePath);
}

This works good, but i want to make sure that two users won’t update at the same time the XML file. I want to lock it somehow.

How can i individually lock the save process for each file?

I could lock the Save function like below, but this will lock all the users, even if they save a different XML file.

private static readonly object _lock = new object();
public void Save(Guid form_Id, IEnumerable<FormData> formData)
{
    lock(_lock)
    {
        XDocument doc = new XDocument();
        foreach (FormData data in formData)
        {
            // Code
        }

        doc.Save(formXMLFilePath);
    }
}
  • 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-17T08:07:41+00:00Added an answer on June 17, 2026 at 8:07 am

    Try something like this:

    FileStream file = new FileStream(formXMLFilePath,FileMode.Create,FileAccress.Write,FileShare.None);
    
    try {
        doc.Save(file);
    } finally {
        file.Close();
    }
    

    This uses a FileStream and we supply a FileShare mode. In our case we are exclusively locking the file so that only we can write to it.

    However, this simply means that subsequent writes will fail as there is a lock.

    To get around this problem I tend to have a queue and a separate thread who’s only job is to process the queue and write to the file system.

    However another solution is to simply try and access the file, and if that fails wait a little bit then try again. This would let you do something akin to lock where it waits until it can access and then proceeds:

    private static bool IsLocked(string fileName)
    {
        if (!File.Exists(fileName)) return false;
    
        try {
            FileStream file = new FileStream(fileName,FileMode.Open,FileAccess.Write,FileShare.None);
    
            try {
                return false;
            } finally {
                file.Close();
            }
        } catch (IOException) {
            return true;
        }
    }
    
    public void Save(Guid form_Id, IEnumerable<FormData> formData)
    {
        while (IsLocked(formXMLFilePath)) Thread.Sleep(100);
    
        FileStream file = new FileStream(formXMLFilePath,FileMode.Create,FileAccress.Write,FileShare.None);
    
        try {
            doc.Save(file);
        } finally {
            file.Close();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application which is supposed to retrieve data from a Website every
I have an application which receives data from a news website (through rss) and
i have created an application which uploads file on a website. while debugging through
i have website of application which sells applications online.on the home page i want
I have an MVC application which is my marketing website, and two other regular
I have an application which serves more than one website. Similar to Stack Exchange,
I have a windows application which is published on a website and can be
I have a silverlight application which I load inside a an asp.net website via
I have a .NET 4 Website (not application) in which I'm trying to use
I'm building an application which I'd like to have it connect to a website

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.