2

I am a new to Programming. I am creating a XML file using windows forms and the name of my XML file is the name field text-box text of windows forms, Its working fine but if the file is already available i want to give new name but i am able to give different name only once. For example if 'dog.xml' is already there then i am able to create dog1.xml file then whenever i am creating any new file the content of the 'dog1.xml' file replaced with new file content but i want to create 'dog11.xml' or 'dog2.xml' file

private void btnSave_Click(object sender, EventArgs e)
{
    path = rtxtName.Text + ".xml";//name of a xml file is name of WPF 'name' field 
    doc = new XmlDocument(); //Here i am creating the xmldocument object
    doc.CreateTextNode(path);
    if (!System.IO.File.Exists(path))//if there is no file exists then
    {
        CreateNewXMLDoc();
    }
    else
    {
        path = rtxtName.Text + "1.xml"; //If the file is already avaliable          
        CreateNewXMLDoc();
    }
}

public void CreateNewXMLDoc() //This method is for creating my xml file
{
    XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", "UTF-8", "yes");
    XmlComment comment = doc.CreateComment("This is a generated XML file");
    doc.AppendChild(declaration);
    doc.AppendChild(comment);
    doc.AppendChild(doc.CreateElement("root"));
}
3
  • 3
    Do you really think your problem is related with xml? Commented Sep 28, 2012 at 6:45
  • 1
    Your code is working fine on how to change name, where is your problem? Commented Sep 28, 2012 at 6:48
  • 2
    As far as I can tell, he wants it to be able to save it as something2.xml if something1.xml exists, or something3.xml if the first 2 exist(actually 3, if you include something.xml). Commented Sep 28, 2012 at 6:53

1 Answer 1

5
    private void btnSave_Click(object sender, EventArgs e)
    {
        path = rtxtName.Text;//name of a xml file is name of WPF 'name' field 

        doc = new XmlDocument(); //Here i am creating the xmldocument object

        string tempPath = path;
        int counter = Properties.Settings.Default.Counter;

        while(System.IO.File.Exists(tempPath))
        {
            counter++;
            tempPath = path + counter + ".xml";
        }

        Properties.Settings.Default.Counter = counter;
        Properties.Settings.Default.Save();
        doc.CreateTextNode(path);
        CreateNewXMLDoc();
    }

If you would like to be fancy and follow Microsofts standards, then change the path to this you can build in the something - Copy.xml then something - Copy (1).xml, etc.

tempPath = path + "(" + counter + ")" + ".xml";

EDIT

Updated to keep the counter when the application restarts

Sign up to request clarification or add additional context in comments.

4 Comments

It is working fine but when i close my application then again counter value becomes zero. How to solve this problem?
I didn't know you needed to save the counter, although what happens when a previous one is removed? ie something1.xml doesnt exist anymore, do you want it to continue from the last one (ie something5.xml or try save it as something1.xml again?
Properties.Settings.Default doesn't contain the definition of 'Counter' should i import some namespace?
Go to the properties form of your project, and then click on the settings tab. Then create a counter setting there, of type int.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.