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"));
}