rustyfancy Posted October 27, 2003 Posted October 27, 2003 Hey, I'm using a XML data structure to store data in C# .NET name/phone number rolodex. Any one know off hand how to query this data? I'm new to XML, so code samples will help. Thank you. --Matt Quote
*Experts* Nerseus Posted October 27, 2003 *Experts* Posted October 27, 2003 If you let us know the format of your XML, I could provide a real sample. For now, look at XmlDocument. With it, you can load the XML string and find values by using the SelectSingleNode, SelectNodes and another function I can't remember to query attributes (they're exposed through an Attributes property, that has a method like GetByName or something). -Nerseus Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
rustyfancy Posted October 27, 2003 Author Posted October 27, 2003 The structure of my XML files is... <Table> <Players> <LastName> Sebek </LastName> <FirstName> Matt </FirstName> <Number> 8 </Number> </Players> <Players> <LastName> Samples </LastName> <FirstName> Matt </FirstName> <Number> 10 </Number> </Players> </Table> I have a search textfield. When "8" is entered into the TextBox, I want the first entry to be displayed. When "Matt" is entered in TextBox, I want both entries to be displayed. Quote
*Experts* Nerseus Posted October 27, 2003 *Experts* Posted October 27, 2003 Let's assume you want to search by number, you'll use the XPath of "/Players[Number=8]". So do something like this: XmlDocument x; // Load up x here... //Now find the right player XmlNode player = x.DocumentElement.SelectSingleNode("/Players[Number=" + txtPlayerNumber.Text + "]"); // Now pull out the First and Last names Debug.WriteLine(player.SelectSingleNode("LastName").Value); Debug.WriteLine(player.SelectSingleNode("FirstName").Value); I'm not sure you need .Value on those last 2 lines of code, you might be able to use ToString instead. I'd have to test to be sure. -Nerseus Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.