1

So I have an XML that looks like this...

<?xml version="1.0" encoding="UTF-8"?>

<!--XML Backup.-->
-<Jobs> 
-<Job> 
 <JobName>a</JobName>
   <Source>C:\Users\Public\Pictures\Samplepictures\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","lighthouse.jpg",and "penguins.jpg" 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.

The code I have now is as follows.

   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

            Dim Source = baseDataNode.SelectNodes("descendant::Source")
            Dim Destin = baseDataNode.SelectNodes("descendant::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


            Console.Write(vbCrLf)


            Console.Write(baseDataNode.Name & ": " & baseDataNode.InnerText)
        Next

        Console.Write(vbCrLf)
        Console.Write(vbCrLf)
    Next

Also, I'm curious if the "descendant::" is necessary... If so, what does it do?

2 Answers 2

1

Your nSource and nDestin variables are list of all Source and Destination tags within the document, not just for the current Job. With in your baseDataNode loop you will need to assign new values to those variables. I don't know the Api your working with well but I would try something like:

For Each baseDataNode As XmlNode In baseDataNodes
  Dim nSource = baseDataNode.SelectNodes("descendant::Source")
  Dim nDestin = baseDataNode.SelectNodes("descendant::Destination")
  ...

EDIT: XmlNode.SelectNodes takes a xpath string, "descendant::Source" is an xpath statment that matches all descendants of the current node which are Source tags.

4
  • I'm usin VB to write this in and it's outputting to a console app. But when i try your code it pops up an error for "nSource" & "nDestin"... which says (variable "nDestin/nSource" hides a variable in an enclosing block) Nov 27, 2012 at 21:18
  • yep you need to remove your earlier nSource and nDestin lines as they are no good for how you are using them. If you need them as is change the names of the variable I added and your references to them within the loop. Nov 27, 2012 at 21:35
  • Ok now the problem i have is that the files wont zip. (unless i return my code back to how i have it in my first post) Nov 27, 2012 at 22:48
  • Do you have any more help that you can bestow upon me? Nov 29, 2012 at 15:48
0

For anyone looking for the answer, please have a look at my solution that I figured out.

[CODE] Sub main()

    Dim doc As New System.Xml.XmlDocument
    Dim myJob As New Atr.backup.Job

    doc.Load("C:\users\matt taylor\desktop\backup\backup.xml")
    myLogger = New Logger("C:\users\matt taylor\desktop\backup\" + DateTime.Now.ToString("yyyy-MM-dd") + ".log")

    Console.SetWindowSize(100, 25)

    Dim JobNodes As XmlNodeList = doc.GetElementsByTagName("Job")
    Dim JobNode As XmlNode
    Dim baseDataNodes As XmlNodeList


    For Each JobNode In JobNodes
        baseDataNodes = JobNode.ChildNodes()


        For Each baseDataNode As XmlNode In baseDataNodes



            myJob.jobName = JobNode.SelectSingleNode("JobName").Name
            myJob.jobSource = JobNode.SelectSingleNode("Source").Name
            myJob.jobDestination = JobNode.SelectSingleNode("Destination").Name
            myJob.jobTimeStamp = JobNode.SelectSingleNode("Timestamp").Name


            Dim Source = JobNode.SelectNodes("Source")
            Dim Destin = JobNode.SelectNodes("Destination")

            Using zip As New ZipFile()
                For Each item As System.Xml.XmlNode In Source
                    zip.AddFile(item.InnerText, JobNode.SelectSingleNode("JobName").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)
            myLogger.Log(baseDataNode.Name & ": " & baseDataNode.InnerText)
        Next

        Console.Write(vbCrLf)
        Console.Write(vbCrLf)
    Next

    Console.Read()
End Sub[/CODE]

and then there is a public class named JOB i made for some of the routines.

[CODE]Public Class Job

    Private name As String
    Private source As String
    Private destination As String
    Private timeStamp As String


    Public Property jobName() As String
        Get
            Return name
        End Get
        Set(value As String)
            name = value
        End Set
    End Property
    Public Property jobSource() As String
        Get
            Return source

        End Get
        Set(value As String)
            source = value
        End Set
    End Property
    Public Property jobDestination() As String
        Get
            Return destination
        End Get
        Set(value As String)
            destination = value
        End Set
    End Property
    Public Property jobTimeStamp() As String
        Get
            Return timeStamp
        End Get
        Set(value As String)
            timeStamp = value
        End Set
    End Property[/CODE]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.