XPath XPathNavigator.Select() query not performing well in a big loop
I have an XML file with 500,000 records and the structure is as follows:
<files>
<file>
<fileName><![CDATA[D:\Inetpub\wwwroot\default.html]]></fileName>
<lastModified>2011-07-01 14:06:25</lastModified>
</file>
</files>
To select a particular record I'm using a XPathNavigator with the query:
xpiOrg = xpnOrg.Select("/files/file[fileName='" & strFileNameCurrent & "']/lastModified")
xpiOrg being a XPathNodeIterator. My aim is to get the 'lastModified' value where the 'fileName' equals the variable strFileNameCurrent, which itself is populated from a XML file (500,000 records) in a loop. Is there any way to speed up the XPath query? Are there any quicker ways to get a matching record from on set of XML from another?
My entire code is like so:
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 Th开发者_如何转开发en
sbNewFiles.Append(strFileNameCurrent).Append("<br>")
Else
If dtLastModifiedCurrent > dtLastModifiedOrg Then
sbModifiedFiles.Append(strFileNameCurrent).Append("<br>")
End If
End If
End While
精彩评论