0

我正在使用 VB XmlWriter.Create() 来获取写入器实例,然后写入 XML,但是一旦创建了 XML 文件,当我第二次运行该程序时,它会覆盖它创建的文件。每次运行程序时如何让它创建一个新文件?还有一种方法可以将文件命名为用户输入的任何内容作为他们的消费者 ID?那么从 txtConsumerID 中获取文本并将文件命名为他们在此文本框中输入的内容?


导入系统
导入 System.Xml

Public 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

结束类

4

1 回答 1

0

您可以通过在文件名中添加时间戳来每次动态创建新文件名,如下所示

C:\Documents and Settings\home\Desktop\Notification_[current_system_time_stamp].xml

以避免替换现有文件。

于 2013-06-17T13:43:21.560 回答