我是编程的新手。我创建一个XML文件使用Windows窗体和我的XML文件的名称是Windows窗体的名称字段文本框文本,它的工作正常,但如果文件已经可用我想给新的名字,但我能够给不同的名字只有一次。例如,如果'dog.xml'已经存在,那么我可以创建dog1.xml文件,然后当我创建任何新文件时,'dog1.xml'文件的内容被替换为新文件内容,但是我想创建' dog11.xml”或‘dog2.xml’文件如何在创建XML时为XML文件提供动态名称
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"));
}
你真的认为你的问题与XML相关? –
您的代码在如何更改名称方面工作正常,您的问题在哪里? –
据我所知,他希望它能够将其保存为something2.xml(如果something1.xml存在),或如果前两个存在(如果包含something.xml,则实际为3)则保存为something3.xml。 – Johan