我使用VB XmlWriter.Create()获取编写器实例然后编写XML,但是一旦创建了XML文件,当我第二次运行程序时它会覆盖它创建的文件。如何在每次运行程序时创建新文件?还有一种方法可以将文件命名为用户输入的消费者ID吗?所以从txtConsumerID中取出文本并将文件命名为它们在此文本框中输入的内容?
Imports System
Imports System.XmlPublic Class Form1 Private Sub saveXML_Click(sender As System.Object, e As System.EventArgs) Handles saveXML.Click Dim settings As New XmlWriterSettings() settings.Indent = True ' Initialize the XmlWriter. Dim XmlWrt As XmlWriter = XmlWriter.Create("C:\Documents and Settings\kck\Desktop\Notification.xml", settings) With XmlWrt ' Write the Xml declaration. .WriteStartDocument() ' Write the root element. .WriteStartElement("PIAlertMonitor") .WriteStartElement("ConsumerID") .WriteString(txtConsumerID.Text.ToString()) .WriteEndElement() .WriteStartElement("MaxAlerts") .WriteString(MaxAlerts.Text.ToString()) .WriteEndElement() .WriteStartElement("Notification") .WriteStartElement("MailTo") .WriteStartElement("eMail") .WriteString(txteMail.Text.ToString()) .WriteEndElement() ' The end of this person. .WriteEndElement() ' Close the XmlTextWriter. .WriteEndDocument() .Close() End With MessageBox.Show("XML File Saved") End Sub
结束课程
答案 0 :(得分:0)
您可以每次动态创建一个新文件名,方法是在文件名中添加时间戳,如下所示
C:\ Documents and Settings \ home \ Desktop \ Notification_ [current_system_time_stamp] .xml
避免替换现有文件。