tags:

views:

92

answers:

4

I have a list of xml elements that i need to search for specific types of elements that beging with the text "UserName". The elements i also have a number after the "UserName" text so they look like this:

I need to find each UserName element in this list but since each one is different due to the number at the end of each one's name I haven't been able to find a way to do this. What i was hoping for was a wildcard or something but that's not working for me.

Any help would be appreciated.

<Profile>
   <Roles23></Roles>
   .
   .
   .
   <Rolese14></Roles>
   .
   .
   <Roles06></Roles>

   etc
   .

 </Profile>
A: 

Please take 15 minutes to understand LINQ to XML! It will take care of such needs easily. If you are not using .NET then take a look at XPATH.

Andrew Siemer
+4  A: 

Using XML like this:

<root>
 <user1></user1>
 <user2></user2>
 <user3></user3>
</root>

This XPATH expression will work: /root/*[starts-with(name(), 'user')]

John Saunders
A: 

Given the poor XML code you provided, it's really difficult to give an answer. Please read up on XPath, the answer you are looking for can certain be found on these two pages:

http://www.w3schools.com/XPath/xpath_syntax.asp

http://www.w3schools.com/XPath/xpath_functions.asp

Cambium
A: 

I would suggest you just have one element of "UserName" not unique items.

/UserName>

going that route you could do something like this:

dim doc as new Xml.XmlDocument doc.Load("YourFile.xml")

dim nodes as Xml.XmlNodeList = doc.FirstChild.SelectNodes("UserName")

eschneider