所以,我有这个程序从用户那里收集“源”、“目标”和“工作名称”,然后以这种方式将这些信息添加到 XML 文件中
-<Jobs> -<Job> <JobName>a</JobName>
<Source>C:\Users\Public\Pictures\SamplePictures\Chrysanthemum.jpg</Source>
<Source>C:\Users\Public\Pictures\Sample Pictures\Desert.jpg</Source>
<Source>C:\Users\Public\Pictures\Sample Pictures\Hydrangeas.jpg</Source>
<Destination>C:\Users\Public\Pictures\Sample Pictures\a.zip</Destination>
<Timestamp>11/23/2012 3:43:42 PM</Timestamp> </Job>
但是,我的问题来自我第二次运行该程序时。我希望它只收集程序中输入的新信息并附加(添加到)XML 文件。但是,当我第二次运行程序时,xml 文件仅更新单个文件的源节点(程序读取的最后一个文件)
有什么帮助吗?下面是我的 fJobDup() 函数代码。
Dim doc As New XmlDocument()
doc.Load("C:\Users\Matt Taylor\Desktop\Backup\Backup.xml")
Dim job As XmlElement = doc.CreateElement("Job")
Dim jName As XmlElement = doc.CreateElement("JobName")
Dim jsource As XmlElement = doc.CreateElement("Source")
Dim jdestin As XmlElement = doc.CreateElement("Destination")
Dim jtime As XmlElement = doc.CreateElement("Timestamp")
job.AppendChild(jName)
job.AppendChild(jsource)
job.AppendChild(jdestin)
job.AppendChild(jtime)
doc.DocumentElement.AppendChild(job)
jName.InnerText = JobName.Text.ToString()
Dim filesEnum3 As IEnumerator
filesEnum3 = OpenFileDialog1.FileNames.GetEnumerator()
While filesEnum3.MoveNext
jsource.InnerText = filesEnum3.Current
End While
jdestin.InnerText = boxDestin.Text.ToString()
jtime.InnerText = Now()
doc.Save("C:\Users\Matt Taylor\Desktop\Backup\Backup.xml")
我已经检查了文件是否存在。当我单击“完成”按钮时,这会在最后出现。
If Dir("C:\Users\Matt Taylor\Desktop\Backup\Backup.xml") <> "" Then
fjobdup()
Else
fJob()
End If
fJobDup() 函数是我在此线程顶部列出的函数。
下面的代码是 fJob() 函数。
Dim Backxml As New XmlWriterSettings()
Backxml.Indent = True
Dim xmlwrt As XmlWriter = XmlWriter.Create =
("C:\Users\MattTaylor\Desktop\Backup\Backup.xml", Backxml)
Dim filesEnum2 As IEnumerator
filesEnum2 = OpenFileDialog1.FileNames.GetEnumerator()
With xmlwrt
.WriteStartDocument()
.WriteComment("XML Backup.")
.WriteStartElement("Jobs")
.WriteStartElement("Job")
Dim jName As String = JobName.Text.ToString
.WriteElementString("JobName", jName.ToString)
While filesEnum2.MoveNext
.WriteStartElement("Source")
.WriteString(filesEnum2.Current)
.WriteEndElement()
End While
.WriteStartElement("Destination")
.WriteString(boxDestin.Text)
.WriteEndElement()
.WriteStartElement("Timestamp")
.WriteString(Now())
.WriteEndElement()
.WriteEndDocument()
.Close()
End With
如果更多代码会有所帮助。这是用于将文件添加到列表框(名为 boxSource)的按钮的代码。
OpenFileDialog1.ShowDialog()
Dim filesEnum As IEnumerator
filesEnum = OpenFileDialog1.FileNames.GetEnumerator()
While filesEnum.MoveNext
boxSource.Items.Add(filesEnum.Current)
End While
End Sub