在XML

2012-11-27 44 views
1

分離拉鍊任務所以我有一個看起來像這樣的XML ...在XML

<?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> 

我想拉上被標記爲「工作」每一父節點。所以第一個拉鍊將是「a.zip」,裏面有「tulips.jpg」,「lighthouse.jpg」和「penguins.jpg」 - 位於目的地。

而第二拉鍊將是「b.zip」與相應的文件。

現在,我正在朝着正確的目的地「a.zip」和「b.zip」 - 但每個壓縮包含的所有文件。

我現在的代碼如下。

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 

此外,我很好奇,如果「descendant ::」是必要的......如果是這樣,它是做什麼的?

回答

1

你n源和nDestin變量都源和目標標籤的列表中的文件中,而不僅僅是當前的工作。在你的baseDataNode循環中,你需要爲這些變量賦新值。我不知道有很好的工作API,但我會嘗試這樣的:

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

編輯: XmlNode.SelectNodes需要一個xpath字符串「descendant::Source」是一個XPath statment說的所有後代匹配當前節點是源標籤。

+0

我全光照VB在寫這一點,它輸出到一個控制檯應用程序。但是,當我嘗試你的代碼它會彈出一個錯誤「n源」和「nDestin」 ...它說(變量「nDestin/n源」隱藏的變量在封閉塊) – MaylorTaylor

+0

是的,你需要刪除較早n源和nDestin因爲它們對你如何使用它們沒有好處。如果您需要它們,請更改我添加的變量的名稱以及您在循環中對它們的引用。 –

+0

好了,現在我遇到的問題是文件不會壓縮。 (除非我將我的代碼返回到我的第一篇文章中) – MaylorTaylor

0

爲尋找答案,請看看我的解決方案,我想通了。

[CODE]子主()

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] 

,然後有一個名爲JOB我對於一些例程由一個公共類。

[CODE]公共類職位

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]