Zipping files within a XML

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 1385 Views - Last Post: 30 November 2012 - 04:38 PM Rate Topic: -----

#16 maylortaylor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 26-November 12

Re: Zipping files within a XML

Posted 30 November 2012 - 10:29 AM

I think the xml means the variable that i have the xml document loaded into.

So where i have
Dim doc = Xdocument.Load("C:\users\matt taylor\desktop\backup\backup.xml")


i would say

For Each j In doc...<job>
                        Dim dest = j...<Destination>.FirstOrDefault().Value
                        If dest IsNot Nothing Then
                            For Each Source In j...<Source>
                                zip.AddFile(Source.value, "Archive_" & DateString)
                            Next
                        End If


However, this leaves a few errors.
1. 'source in j...<source>' part says "value of type system.xml.linq.xelement cannot be converted to xml.nodelist"

2. '.addfile(source.value,' part says 'value' is not a member of xml.nodelist
Was This Post Helpful? 0
  • +
  • -

#17 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2267
  • View blog
  • Posts: 9,482
  • Joined: 29-May 08

Re: Zipping files within a XML

Posted 30 November 2012 - 10:50 AM

Example
Module Module1

  Sub Main()
    Dim doc = <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>
                  <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>
                  <Destination>C:\Users\Public\Pictures\demo photo\b.zip</Destination>
                  <Timestamp>11/26/2012 6:18:19 PM</Timestamp>
                </Job>
              </Jobs>

    For Each job In doc...<Job>
      Dim dest = job...<Destination>.FirstOrDefault().Value
      If dest IsNot Nothing Then
        For Each Source In job...<Source>
          Console.WriteLine(Source.Value)
        Next
        Console.WriteLine()
      End If
    Next
  End Sub

End Module



Was This Post Helpful? 1
  • +
  • -

#18 maylortaylor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 26-November 12

Re: Zipping files within a XML

Posted 30 November 2012 - 11:00 AM

Ok. i get how this xml literal works now.

However, i am trying to load my XML file from a file location on the computer. I'm not making it in this program. I am just reading the XML file, finding the jobname, source,and destination - then i am zipping the files from the source nodes, puting the zip file (named after 'jobname') and storing it at the destination location.

So, how do i read the files from another file location with literals
Was This Post Helpful? 0
  • +
  • -

#19 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2267
  • View blog
  • Posts: 9,482
  • Joined: 29-May 08

Re: Zipping files within a XML

Posted 30 November 2012 - 11:06 AM

Why do you think it only works on the embedded xml?

DIC Site Search
Was This Post Helpful? 0
  • +
  • -

#20 maylortaylor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 26-November 12

Re: Zipping files within a XML

Posted 30 November 2012 - 11:26 AM

Well, i don't think that...

I actually have it loaded in now. Just have a few minor issues i feel. Have a look at this and let me know where the issues are.

The one thing i'm lost on is where i set JobNodes to a value.

It was something similar to JobNodes = doc.getelementsbytag("Job")

but i feel that this step might be invalid while doing it the XML Literal way
Sub main()

        Dim myJob As New Atr.backup.Job

        Dim doc = Xdocument.Load("C:\users\matt taylor\desktop\backup\backup.xml")
        

        Dim JobNodes As XmlNodeList
        Dim JobNode As XmlNode
        Dim baseDataNodes As XmlNodeList


        JobNodes = doc.

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


            For Each baseDataNode As XmlNode In baseDataNodes

                myJob.jobName = baseDataNode.SelectNodes("JobName").ToString
                myJob.jobSource = doc.Element("Source").ToString
                myJob.jobDestination = doc.Element("Destination").ToString
                myJob.jobTimeStamp = doc.Element("Timestamp").ToString

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

                 Using zip As New ZipFile()
                    For Each j In doc...<Job>
                        Dim dest = j...<Destination>.FirstOrDefault().Value
                        If dest IsNot Nothing Then
                            For Each s In j...<Source>
                                zip.AddFile(s.Value, "Archive_" & DateString)
                            Next
                        End If

                    Next

                    For Each j In doc...<Job>
                        Dim dest = j...<destination>.FirstOrDefault().Value
                        If dest IsNot Nothing Then
                            For Each d In j...<Destination>
                                zip.Save(d.Value)
                            Next
                        End If
                    Next
                End Using

                Console.Write(vbCrLf)

                Console.WriteLine(myJob.jobName)
next
next
end sub

Was This Post Helpful? 0
  • +
  • -

#21 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2267
  • View blog
  • Posts: 9,482
  • Joined: 29-May 08

Re: Zipping files within a XML

Posted 30 November 2012 - 12:05 PM

Why do go test out your reasoning? 15 minutes or so max.
That's call Experimental Research.
Was This Post Helpful? 0
  • +
  • -

#22 maylortaylor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 26-November 12

Re: Zipping files within a XML

Posted 30 November 2012 - 12:58 PM

because this is the first program i've written in 4 years and the only one in VB. I'm ignorant of how any (or at least most) of these pieces work seperately, let alone together. So, when i try to figure it out..i'm left to just plugging things in that will hopefully work well
Was This Post Helpful? 0
  • +
  • -

#23 maylortaylor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 26-November 12

Re: Zipping files within a XML

Posted 30 November 2012 - 04:38 PM

I got it working guys. Thanks for all your help. seriously.

I still have some minor issues as always. But that will be fixed another day soon. For now, here is the code that got it working how i wanted.

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")
        

        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)
                            Next

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

        Console.Read()
    End Sub

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2