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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T12:13:49+00:00 2026-06-05T12:13:49+00:00

I am new to web programming (C# in Visual Web Developer) and my non-C

  • 0

I am new to web programming (C# in Visual Web Developer) and my non-C programming skills are a little rusty too.

I have created a table where some of the cells prompt for user input and, once the input is given, the input replaces the prompt. Thus the table can only be annotated by the first person to access the page. Later the annotated page needs to be made available for others to view it so I need the page to load without the prompts after they have been done for the first time. To do this I am (trying) to identify the user so that that person gets the editable page, all of the edits are saved to an xml file, and if another user runs the page the table settings get read back out of the xml file of edits.

I am having trouble consistently writing to the xml file. Specifically I seem to sometimes create more than one process that is accessing the file and I throw a runtime exception when my code tries to update it.

Since I didn’t want a new file being created on each page load I thought a static class was the way to go. Here is the code:

static class XMLReaderWriter
{
    static String fileLocation = "D:\\WebApp\\dashboard.xml";
    static XMLReaderWriter()
    {
        FileStream fs = File.Create(fileLocation);
        if (File.Exists(fileLocation))
        {
            // The opening tag
            writeToFile(fileLocation, "<Dashboard>\n");
        }
        else
        {
            Exception e = new Exception("Failed to create " + fileLocation);
            throw e;
        }
    }
    public static void writeXML(String xml)
    {
        if(File.Exists(fileLocation))
        {
            writeToFile(fileLocation, xml);
        }
        else
        {
            File.Create(fileLocation);
            writeToFile(fileLocation, xml);
        }
    }

    private static void writeToFile(String fileLocation, String xml)
    {
        StreamWriter sw = new StreamWriter(fileLocation, true);
        sw.WriteLine(xml);
        sw.Close();
        sw.Dispose();
    }

    public static string readXML(String trendID)
    {
        StringBuilder result = new StringBuilder("");
        if (File.Exists(fileLocation))
        {
            XDocument xDoc = XDocument.Load(fileLocation);
            var image = from id in xDoc.Descendants(trendID) select new
                        {
                            source = id.Attribute("image").Value
                        };
            foreach (var imageSource in image)
            {
                result.AppendLine(imageSource.source);
            }
        }
        return result.ToString();
    }

    public static void done()
    {
        // The closing tag
        writeToFile(fileLocation, "</Dashboard>");
    }
}

and here is where I am calling the methods:

XMLReaderWriter.writeXML("\t<trend id=\"" + trendID +"\">\n\t\t" + innerHTML + "\" />\n\t</trend>");

and finally there is a submit button to add the closing tag to the xml file:

<asp:Button runat="server" Text="Submit Changes" OnClick="Submit_Click" />

protected void Submit_Click(Object sender, EventArgs e)
{
    XMLReaderWriter.done();
}

Sometimes everything works just fine — although I seem to be producing malformed xml. But most of the time I get more than one process accessing the xml file.

Any advice is appreciated.

Regards.

  • 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-05T12:13:51+00:00Added an answer on June 5, 2026 at 12:13 pm

    Web programming means working in a multi-threaded environment.

    Each request from the browser to the Web server is a separate thread.

    That’s why sometimes your file can’t be accessed, since it’s possible that other request (say it thread) had an exclusive lock on it.

    Another point is using statement should be your friend, so replace:

    StreamWriter sw = new StreamWriter(fileLocation, true);
    sw.WriteLine(xml);
    sw.Close();
    sw.Dispose();
    

    … with:

    using(StreamWriter sw = new StreamWriter(fileLocation, true))
    {
        sw.WriteLine(xml);
    }
    

    This is an equivalent to a try-finally block and it calls Dispose method for any implementation of IDisposable. So in the case your block fails, it’ll be always calling Dispose().

    And above comment it’s fine to mention that if, during some operation in some thread something went wrong, a lock to the file will remain on it forever, until IIS application is stopped or application pool gets recycled.

    Summary:

    1. In your case, more than a thread could be trying to write into the same file.
    2. Some error could leave some lock in affected files.

    Solution: Never work with files in a Web application, there’re better ways of saving data: databases – these solve a lot of problems in concurrent scenarios!! –.

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

Sidebar

Related Questions

I am new to web programming and Visual Web Developer. I have made a
I'm a bit new to web-programming. Using DHTML 3.0 Visual Designer I created a
I am new to web programming...I have been asked to create a simple Internet
I'm new to web programming, so I need some help. I am writing a
I'm new to Django and Web programming in general. Have googled but could not
I'm relatively new to web programming. New to Javascript, PHP, Ajax, etc. I have
I'm new to web programming and I have the following form that I wish
I'm relatively new to web-programming in general so my question may seem a little
I am fairly new to web programming, I have mainly used java to create
I am a college student slightly new to web programming and lately I have

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.