Connecting Tech Pros Worldwide Help | Site Map

return a name of a node in C#

Newbie
 
Join Date: Dec 2008
Posts: 4
#1: Dec 16 '08
hello, I have an xml file and I want to check if the string input in a textbox exist in a node but I dont know what to do.

- For example:
<equal>
<hdd>HardDiskType></hdd>
<hdd>HDD</hdd>

</equal>

a) When I input HDD or HardDiskType in the textbox, the string will match and return true
b) When I input a different string, like "123", it will obviously not match with any string in the tree and therefore return false

A little more difficult problem:
when a) occured, the system will return the name of the father node (in this example it will be "equal")

Thanks in advance.
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,629
#2: Dec 16 '08

re: return a name of a node in C#


sorry, I'm not # enough for C#. though I can help with XPath expressions.

regards
Newbie
 
Join Date: Dec 2008
Posts: 4
#3: Dec 16 '08

re: return a name of a node in C#


well, part of my problem is dealing with xpath (which I'm pretty clueless, since I only know some basic XML), so please help me create a proper xpath :D
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,629
#4: Dec 16 '08

re: return a name of a node in C#


explain the xpath problem (or what you want the xpath to do) and we'll get it on.
Newbie
 
Join Date: Dec 2008
Posts: 4
#5: Dec 16 '08

re: return a name of a node in C#


thanks, for example: i want to create a xpath that return the tag name, uh, like return "hdd" when input HardDiskType or HDD; if possible, return "equal" also

<equal>
<hdd>HardDiskType></hdd>
<hdd>HDD</hdd>
</equal>
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,629
#6: Dec 16 '08

re: return a name of a node in C#


let's cut it down into pieces:
Quote:

Originally Posted by Force88 View Post

a xpath that return the tag name

local-name(*)
Quote:

Originally Posted by Force88 View Post

when input HardDiskType or HDD

*[text() = 'HDD' or text() = 'HardDiskType']
Quote:

Originally Posted by Force88 View Post

if possible, return "equal" also

-- not at the same time (only when you have a node-set as return)
parent::*

now for the homework: put it all together:

regards
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,066
#7: Dec 16 '08

re: return a name of a node in C#


What version of the .NET framework are you using?
Newbie
 
Join Date: Dec 2008
Posts: 4
#8: Dec 16 '08

re: return a name of a node in C#


thanks Dormilich, my problem is now solved.

Im using .Net ver 3.0, included in Visual Studio 2008

by the way, how can I populate the file in a dropdownlist ? I create a xmldatasource, bind it to the dropdownlist, the list did show correct lines accord to the nodes in xml file, but it didnt show the value ("hdd" or "HardDiskType") but "System.blah blah blah instead
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,066
#9: Dec 16 '08

re: return a name of a node in C#


I was going to suggest looking into using LINQ to help you but you seem to have solved your problem.

As for the DropDownList displaying the text, it looks like you can provide an XSL to the XmlDataSource to transform the XML data before it is displayed by the DropDownList.

You can also filter the data by setting the XmlDataSource's XPath property to an XPath expression to filter the XML data after the data is loaded and transformed.

-Frinny
Reply