On 2004-09-14, Mark <fi******@idonotlikejunkmail.umn.edu> wrote:
I have a single XML file containing forty records, with no child nodes. For
example, imagine a list of people's names, and birthdates with SSN.
I'd like to find the row of a person with SSN of 123456789. What's the
quickest way to:
1. Load the file from disk.
2. Find the "record" I want.
3. Walk through the other "fields" in this record to get birthdate, first
name and last name for this person?
For example, should I load the file into a DataSet, or use the XMLDocument
class? I will NOT be binding the data to a DataGrid or similar.
With only forty records, speed probably shouldn't be your biggest
concern. Forty records are going to be read pretty quickly no matter
which class you use.
Still, for the general question, I'd say both of the above are the wrong
choice. Both require the entire file to be parsed into classes, and you
only want a little bit of the file. An XmlReader is probably the
fastest solution, but in this case I'd use an XPathNavigator, which is
faster than an XmlDocument or DataSet but still trivial to parse out
information from...
Imports System.Xml.XPath
Dim doc as new XPathDocument(filename)
Dim navigator as XPathNavigator = doc.CreateNavigator()
Dim iter As XPathNodeIterator = navigator.Select("/person[@ssn='123456789']")
If iter.MoveNext Then
Dim ssn As String = iter.Current.GetAttribute("ssn", "")
Dim fname As String = iter.Current.GetAttribute("fname", "")
...