所以我有一个看起来像这样的 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>
我希望每个标有“工作”的父节点都被压缩。因此,第一个 zip 将是“a.zip”,其中包含“tulips.jpg”、“lighthouse.jpg”和“penguins.jpg” - 位于目的地。
第二个 zip 将是带有相应文件的“b.zip”。
现在,我在正确的目的地得到“a.zip”和“b.zip”——但每个 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
另外,我很好奇“后代::”是否是必要的……如果是这样,它有什么作用?