使用XmlWriter.Create(),如何让它不覆盖它正在创建的文件?

时间:2013-06-17 12:43:33

标签: xml visual-studio-2010 visual-studio xmlwriter

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


Imports System
Imports 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

结束课程

1 个答案:

答案 0 :(得分:0)

您可以每次动态创建一个新文件名,方法是在文件名中添加时间戳,如下所示

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

避免替换现有文件。