我有一百个类似于下面示例的XML,我想在VB.NET中创建一个将这些文件合并为一个的代码。
<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 :(得分:0)
我想出了一个解决方案,我必须创建一个函数来合并输出文件,另一个创建要合并的文件列表,以及我在这里找到的最少但并非最不重要的xsl:{{ 3}}感谢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>