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

  • Home
  • SEARCH
  • 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 7830151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T11:05:52+00:00 2026-06-02T11:05:52+00:00

Hi my aim is to allow the xml file to write a new xml

  • 0

Hi my aim is to allow the xml file to write a new xml document if it does not exist and if it does exist then add elements to the xml file. The issue is that if gives me an error saying “xwriter does not exist in the current context” after the else if() statement. Thanx

so far i have tried this but its not working.

     private void button1(object sender, EventArgs e)
    {

        if (!File.Exists("doc.xml"))
        {
            XmlTextWriter xwriter = new XmlTextWriter("doc.xml", Encoding.UTF8);
            XmlWriterSettings settings = new XmlWriterSettings();
        }

        else if (File.Exists("doc.xml"))
        {
            xwriter.Formatting = Formatting.Indented;
            xwriter.WriteStartDocument();
            xwriter.WriteStartElement("myCourse");
            xwriter.WriteString("Test");
            xwriter.WriteEndElement();
        }

    }
  • 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-02T11:05:54+00:00Added an answer on June 2, 2026 at 11:05 am

    You have two primary problems

    1) Your else statement tries to use a variable that is declared and created in the if statement. That is, xwriter simply does not exist in the else, because it is localised to the scope of the if branch of the code. To use it in the else statement as well, it must be declared before the if statement, so that it is available to both the if and else branches of the code.

    2) You can’t append to an XML file. If you create the xwriter outside the if statement, the code will compile and run, but your else block of code will simply overwrite the existing contents of the xml file.

    To append to xml as you desire you need to open the file, seek to the location where the end tag of the root/document element starts, and then write using an xml writer so that it overwrites the end tag and forms new, valid xml. To close the file you must then append raw text that adds a new end tag for the root/document element so that the xml file remains valid.

    Another (less efficient) approach is to load the XML from the file, and then rewrite the existing contents as well as the new data.

    (You also don’t need to check the existence of the file twice)

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

Sidebar

Related Questions

My aim is to stream a .htm file via Response.WriteFile(Sample.htm); and then access a
we're looking at add a new feature to our site which would allow users
AIM : Convert a resultset to XML and assign the result to a variable
my aim is to invoke a shell script from a PHP program and then
My aim is to list all elements of the array a whose values are
I need to setup a bill pay system to allow for 1 transaction that
I have a website, where I allow other developers to host content. My aim
Basically, I aim to allow my user to specify an ip range for access
I have a page setup where the main aim is to allow users to
I was initially going to use a signed serialized xml file to store license

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.