我有一个包含500,000条记录的XML文件,其结构如下:
<files>
<file>
<fileName><![CDATA[D:\Inetpub\wwwroot\default.html]]></fileName>
<lastModified>2011-07-01 14:06:25</lastModified>
</file>
</files>
要选择特定记录,我正在使用带有查询的XPathNavigator:
xpiOrg = xpnOrg.Select("/files/file[fileName='" & strFileNameCurrent & "']/lastModified")
xpiOrg是一个XPathNodeIterator。我的目标是获取'lastModified'值,其中'fileName'等于变量strFileNameCurrent,它本身是从循环中的XML文件(500,000条记录)填充的。有没有办法加快XPath查询?有没有更快捷的方法从另一个XML集中获取匹配记录?
我的整个代码是这样的:
Dim xtrCurrent As New XmlTextReader("C:\xxx\_files.xml")
Dim xpdCurrent As New XPathDocument(xtrCurrent)
Dim xpnCurrent As XPathNavigator = xpdCurrent.CreateNavigator()
Dim xpiCurrent As XPathNodeIterator = xpnCurrent.Select("/files/file")
Dim xpiCurrentChildren As XPathNodeIterator
Dim xtrOrg As New XmlTextReader("C:\xxx\files.xml")
Dim xpdOrg As New XPathDocument(xtrOrg)
Dim xpnOrg As XPathNavigator = xpdOrg.CreateNavigator()
Dim xpiOrg As XPathNodeIterator
Dim strFileNameCurrent As String
Dim dtLastModifiedCurrent As DateTime
Dim dtLastModifiedOrg As DateTime
Dim sbNewFiles As New StringBuilder
Dim sbModifiedFiles As New StringBuilder
While xpiCurrent.MoveNext()
'get fileName and lastModified from current node
xpiCurrentChildren = xpiCurrent.Current.SelectChildren(XPathNodeType.Element)
xpiCurrentChildren.MoveNext()
strFileNameCurrent = xpiCurrentChildren.Current.Value
xpiCurrentChildren.MoveNext()
dtLastModifiedCurrent = Convert.ToDateTime(xpiCurrentChildren.Current.Value)
'did it exist in the org xml list?
xpiOrg = xpnOrg.Select("/files/file[fileName='" & strFileNameCurrent & "']/lastModified")
While xpiOrg.MoveNext()
dtLastModifiedOrg = Convert.ToDateTime(xpiOrg.Current.Value)
End While
If xpiOrg.Count = 0 Then
sbNewFiles.Append(strFileNameCurrent).Append("<br>")
Else
If dtLastModifiedCurrent > dtLastModifiedOrg Then
sbModifiedFiles.Append(strFileNameCurrent).Append("<br>")
End If
End If
End While