XML Document - find one row

  • Thread starter Thread starter Mark
  • Start date Start date
M

Mark

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.

Thanks in advance.
Mark
 
Can someone give me some recommendations on source control software for
dotnet projects.

Regards
Dan
 
Oops sorry for replying to your thread. I am not used to using this mail
client.

Regards
Daniel
 
This is my recommendation, assuming we have some XML like this:
<root>
<person name="Bart" ssn="123" />
<person name="Marge" ssn="234" />
</root>


<snippet>
string filename = "C:\people.xml", ssn = "123";
XmlDocument doc = new XmlDocument();
doc.Load(filename);
XmlNode resultNode = doc.DocumentElement.SelectSingleNode
(string.Format("person[@ssn='{0}']", ssn));
if (null != resultNode)
Console.WriteLine("SSN number {0} belongs to {1}!", ssn,
resultNode.Attributes ["name"].InnerText);
else
Console.WriteLine("SSN number {0} does not exist.", ssn);
</snippet>
 
Mark,

If you want the quickest way, then I would structure the data so that
the SSN number is in the parent element for each person. Then, I would use
an XmlTextReader to read the XML sequentially from the file. When you come
across a node with the appropriate SSN, then you will know to read the rest
of the nodes for your information, and to not just skip them.

Hope this helps.
 
Back
Top