为多个文件追加XML文件

时间:2012-11-23 21:34:30

标签: xml vb.net file

所以,我有这个程序从用户那里收集'source','destination'和'job name',然后以这种方式将这些信息添加到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

1 个答案:

答案 0 :(得分:0)

您正在每次运行时创建一个新的XMLDocument。

尝试检查文件是否存在。

看看这是否有帮助..请验证语法一次..

Dim doc as XmlDocument()

If My.Computer.FileSystem.FileExists("C:\Users\Matt Taylor\Desktop\Backup\Backup.xml") Then
    doc.Load("C:\Users\Matt Taylor\Desktop\Backup\Backup.xml")
Else
    Dim doc As New XmlDocument()
End If