Adding multiple files to an XML multiple times
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Adding multiple files to an XML multiple times

  1. #1
    Join Date
    Nov 2012
    Posts
    3

    Adding multiple files to an XML multiple times

    So, I have this program that gathers 'source', 'destination', and 'job name' from a user and then adds this information to an XML file in this type of manner

    Code:
    -<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>
    However, my issue comes from when i run the program a second time around. I want it to just collect the new information inputted in the program and append(add to) the XML file. However, when i run the program a second time the xml file only updates the source nodes for a single file (the last file read by the program).. So i am left with this after the initial run the second time around.

    Code:
    -<Job> 
    <JobName>b</JobName> 
    <Source>C:\Users\Public\Pictures\Sample Pictures\Hydrangeas.jpg</Source> <Destination>C:\Users\Public\Pictures\Sample Pictures\b.zip</Destination> 
    <Timestamp>11/26/2012 10:39:50 AM</Timestamp>
     </Job>
    **************(there should be more source files in the above code....this is my issue.)******************






    i already have a check for if the file exists or not. This comes in at the very end when i click the 'finish' button

    Code:
    If Dir("C:\Users\Matt Taylor\Desktop\Backup\Backup.xml") <> "" Then
            fjobdup()
        Else
            fJob()
        End If
    Where the "fJob()" function writes the xml the first time through. And the "fJobDup()" function should append the xml file and add the new information below the initial info. I'll post these functions below.

    Code:
    Public Function fjobdup() As String
    
            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.ToString
    
            End While
    
            jdestin.InnerText = boxDestin.Text.ToString()
            jtime.InnerText = Now()
    
            doc.Save("C:\Users\Matt Taylor\Desktop\Backup\Backup.xml")
    
        End Function
    Code:
    Public Function fJob() As String
            Dim Backxml As New XmlWriterSettings()
            Backxml.Indent = True
            Dim xmlwrt As XmlWriter = XmlWriter.Create("C:\Users\Matt Taylor\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
    
    
        End Function

  2. #2
    Join Date
    Nov 2012
    Posts
    3

    Re: Adding multiple files to an XML multiple times

    I found the fix for anyone out there with the same issue (or similar)

    It was a simple use of node.appendchild() in a few key places.

    Code:
    Public Function fjobdup() As String
    
            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 jdestin As XmlElement = doc.CreateElement("Destination")
            Dim jtime As XmlElement = doc.CreateElement("Timestamp")
    
            job.AppendChild(jName)
            jName.InnerText = boxJobName.Text.ToString()
    
            Dim filesEnum3 As IEnumerator
            filesEnum3 = OpenFileDialog1.FileNames.GetEnumerator()
    
            While filesEnum3.MoveNext
                Dim jsource As XmlElement = doc.CreateElement("Source")
                job.AppendChild(jsource)
                jsource.InnerText = filesEnum3.Current.ToString
            End While
    
            job.AppendChild(jdestin)
            jdestin.InnerText = boxDestination.Text.ToString()
    
            job.AppendChild(jtime)
            jtime.InnerText = Now()
    
            doc.DocumentElement.AppendChild(job)
    
            doc.Save("C:\Users\Matt Taylor\Desktop\Backup\Backup.xml")
    
        End Function

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center