Link to home
Start Free TrialLog in
Avatar of twickey
twickeyFlag for United States of America

asked on

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"
rss...<var>

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!
Tim
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>
 
   19 
 
   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

Avatar of Christopher Kile
Christopher Kile
Flag of United States of America image

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

http://msdn.microsoft.com/en-us/library/bb384964.aspx

Note that it is EXACTLY like embedding a server property reference into an .ASPX page.
Avatar of twickey

ASKER

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

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.
Avatar of twickey

ASKER

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?
??plant.Element.Name

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

Thanks,
Tim



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">
                         <PLANT>
                             <COMMON>Bloodroot</COMMON>
                             <BOTANICAL>Sanguinaria canadensis</BOTANICAL>
                             <ZONE>4</ZONE>
                             <LIGHT>Mostly Shady</LIGHT>
                             <PRICE>$2.44</PRICE>
                             <AVAILABILITY>031599</AVAILABILITY>
                         </PLANT>
                         <PLANT>
                             <COMMON>Columbine</COMMON>
                             <BOTANICAL>Aquilegia canadensis</BOTANICAL>
                             <ZONE>3</ZONE>
                             <LIGHT>Mostly Shady</LIGHT>
                             <PRICE>$9.37</PRICE>
                             <AVAILABILITY>030699</AVAILABILITY>
                         </PLANT>
                         <PLANT>
                             <COMMON>Marsh Marigold</COMMON>
                             <BOTANICAL>Caltha palustris</BOTANICAL>
                             <ZONE>4</ZONE>
                             <LIGHT>Mostly Sunny</LIGHT>
                             <PRICE>$6.81</PRICE>
                             <AVAILABILITY>051799</AVAILABILITY>
                         </PLANT>
                     </CATALOG>
 
        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"
        Next
 
        plants.Save("plants.xml")
     
    End Sub
 
End Module

Open in new window

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).
Avatar of twickey

ASKER

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!
Tim
ASKER CERTIFIED SOLUTION
Avatar of twickey
twickey
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.