2014-08-27 84 views
0

我喜歡上百個如下例所示的XML,我想在VB.NET中創建一個將這些文件合併爲一個的代碼。將一個文件夾內的XML文件合併到一個XML中

<report> 
<Host>10.10.10.4</Host> 
<Ping>True</Ping> 
<PingResult>True</PingResult> 
<start>27/08/2014 14:11:17</start> 
<end>27/08/2014 14:11:22</end> 
<id>86</id> 
<Total>00:00:05</Total> 
<success>False</success> 
</report> 

<report> 
<Host>10.10.10.5</Host> 
<Ping>True</Ping> 
<PingResult>True</PingResult> 
<start>27/08/2014 14:11:41</start> 
<end>27/08/2014 14:11:58</end> 
<id>86</id> 
<Total>00:00:17</Total> 
<success>False</success> 
</report> 

功能寫個XML:

Private Sub writeReport(result As Boolean, PingResult As Boolean, IpRangePing As Boolean, iSpan As TimeSpan, StartTime As DateTime, EndTime As DateTime, err As Boolean) 
If (Not System.IO.Directory.Exists(Me.FilePath + "\report")) Then 
    System.IO.Directory.CreateDirectory((Me.FilePath + "\report")) 
End If 
Dim writer As New XmlTextWriter(Me.FilePath + "\report\" + Me.Id.ToString + Me.HostName + ".xml", Nothing) 

writer.WriteStartDocument() 

writer.WriteStartElement("report") 

writer.WriteElementString("Host", Me.HostName.ToString) 
writer.WriteElementString("Ping", IpRangePing.ToString) 
writer.WriteElementString("PingResult", PingResult.ToString) 
writer.WriteElementString("start", StartTime.ToString) 
writer.WriteElementString("end", EndTime.ToString) 
writer.WriteElementString("id", Me.Id.ToString) 
writer.WriteElementString("Error", err.ToString) 
writer.WriteElementString("Total", iSpan.Hours.ToString.PadLeft(2, "0"c) & ":" & _ 
       iSpan.Minutes.ToString.PadLeft(2, "0"c) & ":" & _ 
       iSpan.Seconds.ToString.PadLeft(2, "0"c)) 
writer.WriteElementString("success", result.ToString) 
writer.WriteEndElement() 
writer.Close() 
End Sub 

而我的計劃是利用每個和讀取所有XML,並再創建一個新的。

Dim directory As System.IO.DirectoryInfo = New System.IO.DirectoryInfo(Me.FilePath + "\report\") 
Dim xmlFiles As System.IO.FileInfo() = directory .GetFiles("*.xml") 
For Each xmlFile As System.IO.FileInfo In xmlFiles 
    //To Do 
Next 
+0

你能列出一些你已經試過的代碼嗎? – Kat 2014-08-27 19:38:33

回答

0

我想出了一個解決方案,我必須創建一個函數來合併輸出文件,就創建了一個文件列表進行合併,最少但並非最不重要的XSL我發現在這裏:http://p2p.wrox.com/83450-post4.html由於joefawcett

Private Sub MergeReports(filename As String, stylesheet As String) 
Dim doc As XmlDocument = New XmlDocument() 
doc.Load(filename) 

Dim root As XmlElement = doc.DocumentElement 
Dim elemList As XmlNodeList = root.GetElementsByTagName("Item") 
If elemList.Count.Equals(Me.TotalItems) Then 
    'Create the XslTransform and load the style sheet. 
    Dim settings As New XsltSettings(True, True) 
    Dim xslt As New XslCompiledTransform() 
    xslt.Load(stylesheet, settings, Nothing) 
    ' Execute the transform and output the results to a file. 
    xslt.Transform(filename, "C:\NW\out\report\output.xml") 
End If 
End Sub 


Private Sub CreateList() 
Try 
    If (Not System.IO.File.Exists(Me.FilePath + "\report\list.xml")) Then 
    Dim writer As New XmlTextWriter(Me.FilePath + "\report\list.xml", Nothing) 
    writer.WriteStartDocument() 
    writer.WriteStartElement("Data") 
    writer.WriteAttributeString("Root", "Root") 
    writer.WriteStartElement("fileList") 
    writer.WriteElementString("Item", Me.HostName.ToString) 
    writer.WriteEndElement() 
    writer.Close() 
    Else 
    Dim xmlDoc As XmlDocument = New XmlDocument() 
    xmlDoc.Load(Me.FilePath + "\report\list.xml") 
    With xmlDoc.SelectSingleNode("//Data/fileList").CreateNavigator().AppendChild() 
     .WriteElementString("Item", Me.FilePath + "\report\" + Me.HostName.ToString) 
     .Close() 

    End With 
    xmlDoc.Save(Me.FilePath + "\report\list.xml") 
    End If 
Catch ex As Exception 
    Console.WriteLine(ex.Message) 
End Try 
End Sub 

XSL

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:template match="/"> 
    <xsl:element name="{Data/@Root}"> 
    <xsl:apply-templates select="Data/List/Item"/> 
    </xsl:element> 
</xsl:template> 
<xsl:template match="Item">  
<xsl:copy-of select="document(.)"/> 
</xsl:template> 

文件列表:

<Data Root="Root"> 
    <List> 
    <Item>C:\NW\out\report\File1.xml</Item> 
    <Item>C:\NW\out\report\File2.xml</Item> 
    <Item>C:\NW\out\report\File3.xml</Item> 
    </List> 
</Data> 
相關問題