XML Axis element look-up

I want to know if I can use the new XML Axis properties in Visual Basic 9 with variable search criteria. Consider rss...<item>. This will return all elements named item no matter it's hierarchy. I want to run this same query as follows...
Dim var as String = "item"

Is this possible? I want to be able to retrieve an element dynamically based on user input.

The code I attached is from http://steve.emxsoftware.com/LINQ/Xml+Axis+properties+VBNETs+second+cool+Xml+language+construct

Thanks in advance!
1 Public Function GetItems() As IEnumerable(Of XElement)
    2     Dim rss As XElement = <rss version="2.0">
    3                             <channel>
    4                               <title>Scott Gu's Blog</title>
    5                               <link>http://weblogs.asp.net/scottgu/</link>
    6                               <description>Scott Guthrie's Blog on ASP.NET and .NET</description>
    7                               <item>
    8                                 <title>Recipe: Dynamic Site Layout and Style Personalization with ASP.NET</title>
    9                                 <link>http://weblogs.asp.net/scottgu/arc...Personalization-with-ASP.NET--.aspx</link>
   10                                 <pubDate>Sun, 23 Jul 2006 02:19:00 GMT</pubDate>
   11                               </item>
   12                               <item>
   13                                 <title>Upcoming Free ASP.NET Team Webcasts -- Live from Redmond</title>
   14                                 <link>http://weblogs.asp.net/scottgu/arc..._2200_Live-from-Redmond_2200_.aspx</link>
   15                                 <pubDate>Sat, 22 Jul 2006 02:15:00 GMT</pubDate>
   16                               </item>
   17                             </channel>
   18                           </rss>
   20     Return rss.Element("channel").Elements("item")
   21   End Function
After using the Xml literal support within VB 9 to create a trimmed down version of Scotts feed we use the Element and Elements extension methods to select the data that were interested in.  As you can see (line 20) I use the Element() method to select the channel element within the feed and then select all items within the channel element by calling Elements() with a XName of item.  As an alternative VB 9 provides axis properties which allow the above to be expressed using a more compact syntax.
   Return rss...<item>
This expressions uses the descendants axis support in VB 9 which can be interpreted as: give me all item elements underneath the rss element regardless of where they occur in the heirarchy.  VB9 also has support for child axis which can be used to get all elements of a given name directly under a given element.  For example to return all the channel elements underneath the root rss element one could use the following syntax.
   Return rss.<channel>
If your not interested in elements but instead want to get at attributes you can use the attribute axis syntax:
   Return rss.@version 
And finally if you want to grab a particular item you can use the extension indexer to select the item of interest off the resulting sequence like so:
   Return rss...<item>(3)

Open in new window

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Christopher KileCommented:
This link shows how to "embed" variable information into an XML definition, which is exactly what you want to do:


Note that it is EXACTLY like embedding a server property reference into an .ASPX page.
twickeyAuthor Commented:
Thanks for the response. I'm not sure if it's possible to use your suggestion in the way I described. It seems as though it is for dynamically creating XML statements where the question I pose is for using the new Axis properties of Visual Basic 9. Maybe I'm wrong.

The code below doesn't compile. If you see what I'm doing wrong can you alert me? Thanks again.

'this line works
'GetItems = ccr...<Language>.<Text>
'this one doesn't
'GetItems = ccr...<Language>.<<%= elementName %>>

Dim ccr = XDocument.Load(openfilename)
Dim GetItems As IEnumerable(Of XElement)
Dim elementName As String = "Text"
GetItems = ccr...<Language>.<<%= elementName %>>

Open in new window

Christopher KileCommented:
Hmmmm...I'm coming to the conclusion that what you want to do isn't possible, at least not the way you are doing it.  Instead, I suggest you build a big Select statement that allows you to select your search expression instead of trying to substitute a variable selection for an element name.
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

twickeyAuthor Commented:
I apologize for my lack of experience in XML.
As you can see from the code example <Plant> is already known. Could we back this up one step and include Plant in the Select statement? Let's start with Catalog instead as it is the first element.

Instead of...
        Dim q = From plant In plants...<PLANT> _
                Where plant.<COMMON>.Value = "Columbine" _
                Select plant
how about...
        Dim q = From plant In plants...<CATALOG> _
                Where plant.Element.Name = "PLANT" _
                Select plant

What should be used to identify the elements by Name?

Another Select statement would be used on the result to find the  
plant.<COMMON>.Value = "Columbine"


Imports <xmlns="urn:mycompany:examples:plants">
Module Module1
    Sub Main()
        Dim plants = <?xml version="1.0" encoding="ISO-8859-1"?>
                     <CATALOG xmlns="urn:mycompany:examples:plants">
                             <BOTANICAL>Sanguinaria canadensis</BOTANICAL>
                             <LIGHT>Mostly Shady</LIGHT>
                             <BOTANICAL>Aquilegia canadensis</BOTANICAL>
                             <LIGHT>Mostly Shady</LIGHT>
                             <COMMON>Marsh Marigold</COMMON>
                             <BOTANICAL>Caltha palustris</BOTANICAL>
                             <LIGHT>Mostly Sunny</LIGHT>
        Dim q = From plant In plants...<PLANT> _
                Where plant.<COMMON>.Value = "Columbine" _
                Select plant
        For Each item In q
            q.<PRICE>.Value = "$49.99"
            q.<LIGHT>.Value = "Full Sun"
    End Sub
End Module

Open in new window

Christopher KileCommented:
This is something I myself will have to experiment with.  I'll have to wait until I go home to do so, as I work strictly in VS 2003 at work (which does not support LINQ to XML).
twickeyAuthor Commented:
It may help if I identify my problem specifically.
I need to be able to identify a unique position in an XML file and update it per user input in a separate program.

The XML file can be very long and complex. All possible elements are known such that an enormous Switch statement could be built to locate each element however I thought it would be easier to simply pass the element name and it's parent to the query in order to find the value.

So considering we know the parent and child name, how can we build the Select statement to pull the correct data?

I hope this makes sense.

Thanks again!
twickeyAuthor Commented:
I have figured out a solution to my problem. I attached sample code to demonstrate. All it does is go through all the elements in the XML file and retrieve the ones that have a matching name and parent name. I could also include grandparent name if necessary to  narrow results even more specifically.
Then I loop through each "found" element and update its value. The entire XML document will be saved correctly along with the modified element value on Save since a Select statement was used to find elements.

Thanks a lot for your help!

Dim ccr = XDocument.Load(openfilename)
Dim QQ = From cc In ccr.Descendants _
Where cc.Name.ToString = "elementname" AndAlso cc.Parent.Name.ToString = "elementparentname" _
Select cc
For Each item In QQ
item.Value = "whatever"

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Christopher KileCommented:
Nice solution :) I'd come to the conclusion it would look like this, but hadn't had the time to work on it.  You definitely deserve your points back, and congratulations.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.