Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

  • *Experts*
Posted

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

"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
Posted

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.

  • *Experts*
Posted

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

"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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...