私はプログラミングが初めてです。Windows フォームを使用して XML ファイルを作成しています。XML ファイルの名前は、Windows フォームの名前フィールド テキスト ボックス テキストです。正常に動作していますが、ファイルが既に利用可能な場合は、新しい名前を付けたいのですが、与えることができます。違う名前は一度だけ。たとえば、「dog.xml」が既に存在する場合は、dog1.xml ファイルを作成できます。新しいファイルを作成するたびに、「dog1.xml」ファイルの内容が新しいファイルの内容に置き換えられますが、「 「dog11.xml」または「dog2.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"));
}