Separating a zip task in XML

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • maylortaylor
    New Member
    • Nov 2012
    • 72

    Separating a zip task in XML

    So I have an XML that looks like this...
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    
    <!--XML Backup.-->
    -<Jobs> 
    -<Job> <JobName>a</JobName> <Source>C:\Users\Public\Pictures\Sample Pictures\Lighthouse.jpg</Source> <Source>C:\Users\Public\Pictures\Sample Pictures\Penguins.jpg</Source> <Source>C:\Users\Public\Pictures\Sample Pictures\Tulips.jpg</Source> <Destination>C:\Users\Public\Pictures\Sample Pictures\a.zip</Destination>
    <Timestamp>11/26/2012 6:18:00 PM</Timestamp> 
    </Job> 
    -<Job> <JobName>b</JobName> <Source>C:\Users\Public\Pictures\demo photo\1 - Copy.JPG</Source> <Source>C:\Users\Public\Pictures\demo photo\1.JPG</Source> <Source>C:\Users\Public\Pictures\demo photo\2 - Copy.JPG</Source> <Destination>C:\Users\Public\Pictures\demo photo\b.zip</Destination> 
    <Timestamp>11/26/2012 6:18:19 PM</Timestamp> </Job> </Jobs>
    And i want each Parent Node labeled "Job" to be zipped. So the first zip would be "a.zip", with "tulips.jpg","l ighthouse.jpg", and "penguins.j pg" inside - located at the destination.

    And the second zip would be "b.zip" with the respective files.

    right now, i am getting "a.zip" and "b.zip" in the right destinations - BUT each zip contains all the files.

    Code:
    Dim doc As New System.Xml.XmlDocument
            doc.Load("C:\users\matt taylor\desktop\backup\backup.xml")
     Dim nSource = doc.GetElementsByTagName("Source")
    Dim nDestin = doc.GetElementsByTagName("Destination")
     Dim JobNodes As XmlNodeList
            Dim JobNode As XmlNode
            Dim baseDataNodes As XmlNodeList
            Dim bFirstInRow As Boolean
    
            JobNodes = doc.GetElementsByTagName("Job")
            For Each jobNode In JobNodes
                baseDataNodes = JobNode.ChildNodes
                bFirstInRow = True
                For Each baseDataNode As XmlNode In baseDataNodes
                    
                    Using zip As New ZipFile()
    
                        For Each item As System.Xml.XmlElement In nSource
                            zip.AddFile(item.InnerText, "Archive_" & DateString)
                        Next
    
    
                        For Each item As System.Xml.XmlElement In nDestin
                            zip.Save(item.InnerText)
                        Next
                    End Using
    
                        Console.Write(vbCrLf)
                   
    
                    Console.Write(baseDataNode.Name & ": " & baseDataNode.InnerText)
                Next
                
                Console.Write(vbCrLf)
                Console.Write(vbCrLf)
            Next
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    The problem is that you're using every element in the nSource varible which contains every tag that is named source (Line 3). What you want to do is grab only the source elements in the job that it's on.

    Comment

    • maylortaylor
      New Member
      • Nov 2012
      • 72

      #3
      So would i be on the right track with doing this.

      Code:
      For Each baseDataNode As XmlNode In baseDataNodes
      
                      Dim Source = baseDataNode.SelectNodes("Source")
                      Dim Destin = baseDataNode.SelectNodes("Destination")
      
                      Using zip As New ZipFile()
      
                          For Each item As System.Xml.XmlElement In Source
                              zip.AddFile(item.InnerText, "Archive_" & DateString)
                          Next
      
                          For Each item As System.Xml.XmlElement In Destin
                              zip.Save(item.InnerText)
                          Next
                      End Using

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        That looks right at first blush. I don't have VB6 to test the code with but it should be very close, if not exactly, to what you need.

        Comment

        • maylortaylor
          New Member
          • Nov 2012
          • 72

          #5
          Well, the issue i am running into now is that it wont zip any of my files. It's just completely skipping the For Each loops meant to zip each source and destin location

          Comment

          • Rabbit
            Recognized Expert MVP
            • Jan 2007
            • 12517

            #6
            Can you post the full code after the modification?

            Comment

            • maylortaylor
              New Member
              • Nov 2012
              • 72

              #7
              Code:
              Dim doc As New System.Xml.XmlDocument
                      doc.Load("C:\users\matt taylor\desktop\backup\backup.xml")
              
                     
                      Dim JobNodes As XmlNodeList
                      Dim JobNode As XmlNode
                      Dim baseDataNodes As XmlNodeList
              
              
                      JobNodes = doc.GetElementsByTagName("Job")
                      For Each jobNode In JobNodes
                          baseDataNodes = JobNode.ChildNodes()
              
                          For Each baseDataNode As XmlNode In baseDataNodes
              
                              Dim Source = baseDataNode.SelectNodes("descendant::Source")
                              Dim Destin = baseDataNode.SelectNodes("descendant::Destination")
              
                              Using zip As New ZipFile()
              
                                  For Each item As System.Xml.XmlNode In Source
                                      zip.AddFile(item.InnerText, "Archive_" & DateString)
                                  Next
              
                                  For Each item As System.Xml.XmlNode In Destin
                                      zip.Save(item.InnerText)
                                  Next
                              End Using
              
                              Console.Write(vbCrLf)
              
                              Console.Write(baseDataNode.Name & ": " & baseDataNode.InnerText)
                          Next
              
                          Console.Write(vbCrLf)
                          Console.Write(vbCrLf)
                      Next
              
                      Console.Read()
              
              
              
                  End Sub
              Like i said, it just seems to skip right over my "For each item" loops that the Zip uses. The console still prints out all the information correctly..just the zip doesn't get to do its job.

              Comment

              • Rabbit
                Recognized Expert MVP
                • Jan 2007
                • 12517

                #8
                On lines 16 and 17, try taking out the "descendant ::".

                Comment

                • maylortaylor
                  New Member
                  • Nov 2012
                  • 72

                  #9
                  Ya, that was my first thought. ...but it still skips the for each loops. I'm baffling

                  Comment

                  • Rabbit
                    Recognized Expert MVP
                    • Jan 2007
                    • 12517

                    #10
                    I don't see what's wrong and I won't be able to test the code until tomorrow. In the mean time, try outputting counts and values and before and within the loops to see if there's anything to output.

                    Comment

                    Working...