Solved

For Each  - Xpath /VB.Net

Posted on 2009-04-09
6
750 Views
Last Modified: 2013-11-11
Hi,
I am having difficulty getting this to work.  

If I run it with the return value string I get 2 results (which is right) but they both formatted according to the first match.  It I run it without the returnVal then I only get the first match.  Either way its not working as I hoped.

What am I doing wrong?

Snippet:
<products>
  <product code="av" name="AV Training">
      <duration>4 Days</duration>
      <prices>
        <price vehicle="av">2395</price>
        <price vehicle="avno">1875</price>
      </prices>
    </product>
</products>


Happy to try linq - but don't know it very well.
Cheers
Tania
Public Function getPrice ()  As String 
 
   Dim sHireType As String = XPath("prices/price/@vehicle")
   Dim sHire4x4 As String = "(includes use of a 4wd training vehicle)"
   Dim sBYO4x4 As String = "(if you bring your 4wd)"
   Dim sHireAV As String = "(includes use of a 4wd AV)"
   Dim sByoAV As String = "(if you bring your 4wd AV)"
   Dim sBYOMO As String = "<br />(you will need to bring your own motorcycle)"
			  
   Dim sPriceFormat As String = "<li>Price: US$ {0} per Student {1}.</li>" 
   
   Dim MyList As New ArrayList
          MyList = XpathSelect("prices/*")
 
   Dim sPrice As String = XPath("prices/*")
				 
   Dim a as XmlNode
   Dim ReturnVal as String 
   
        For Each a In MyList
     	
             Select Case sHireType
                   Case "av"
		 	'Return String.Format(sPriceFormat, sPrice, sHire4x4)
      		         ReturnVal &= " " & String.Format(sPriceFormat, sPrice, sHireAV)
       		   Case "avno"
       			ReturnVal &= " " & String.Format(sPriceFormat, sPrice, sByoAV)
		       'Return String.Format(sPriceFormat, sPrice, sByoAV)
                  Case "yes"
       			ReturnVal &= " " & String.Format(sPriceFormat, sPrice, sHire4x4)
		       'Return String.Format(sPriceFormat, sPrice, sHire4x4)
    	       End Select
    	  Next a
 	 Return ReturnVal        
  End Function
 
 
 
  <asp:Repeater id="repeater_price" DataSourceId="XmlSource1"  runat="server">
            <ItemTemplate> 
                <%#getDuration()%>
             <%#  getPrice() %>
            </ItemTemplate>  
          </asp:Repeater>

Open in new window

0
Comment
Question by:Tania_Farmer
  • 4
  • 2
6 Comments
 
LVL 39

Expert Comment

by:abel
ID: 24114623
The code is already very helpful, but I'm not sure I understand your question well, can you give the response that you expect from the repeater (i understand you'll have to type it by hand) alongside with the wrong response you're having now?
0
 
LVL 39

Expert Comment

by:abel
ID: 24114727
Think I see your problem (just ran it locally). Your sPrice is the price tag and is a string which is declared like so:

  Dim sPrice As String = XPath("prices/*")

this string is then used inside the for-each and contains the string value of the first element that the xpath matched, which is 2395. To get the price of each node while going through the prices, you can use:
  For Each a In MyList       sPrice = a.FirstChild.Value

which will give you the value of the (first and only) text node inside <price>.

May I ask you why you are using an asp:Repeater and then do the repetition inside a method? Or is it part of a larger whole?

-- Abel --
0
 

Author Comment

by:Tania_Farmer
ID: 24114873
Hi,
Thanks - it works.  It shows:
# Price: US$ 2395 per Student (includes use of a 4wd AV).
# Price: US$ 1875 per Student (includes use of a 4wd AV).

How do I adjust it so the vehicle info is also updated:
# Price: US$ 2395 per Student (includes use of a 4wd AV).
# Price: US$ 1875 per Student (if you bring your 4wd AV).  

<price vehicle="av">2395</price>
<price vehicle="avno">1875</price>

I used the method so I could format and expand the results based on the return.  Its a control and used on several pages.  

I have :

<asp:Repeater id="repeater_price" DataSourceId="XmlSource1"  runat="server">
            <ItemTemplate>
                    <%#getDuration()%>
                    <%#  getPrice() %>
            </ItemTemplate>            
</asp:Repeater>

It returns the 2 prices and the course duration.

T
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 39

Accepted Solution

by:
abel earned 500 total points
ID: 24114998
> How do I adjust it so the vehicle info is also updated:

same story there. You'll have to put that inside the loop (and your can remove the initialization of the variables):

 For Each a In MyList
     sPrice = a.FirstChild.Value
     sHireType = a.Attributes("vehicle").Value


On the other point: if you meant to use the repeater for looping through the prices, you can make it much easier in another way. If you wanted to use the repeater to loop through the products, than the current method is probably the easiest.

-- Abel --
0
 

Author Comment

by:Tania_Farmer
ID: 24115432
Hi,
Thanks  that works great.

By the way what is better to use?  If there is a difference?

Dim myList As IEnumerable = xpathselect("prices/*")

or

Dim myList As New ArrayList
      myList = XpathSelect("prices/*")

How do I make it easier?  Its looping through the prices.  The product info is set and displayed by xmldatasource on the relevent page and then the control is pulled to display the price.

T
0
 
LVL 39

Expert Comment

by:abel
ID: 24118879
> Thanks  that works great.

you're welcome

> By the way what is better to use?  If there is a difference?

in your case, you do not need to use either. You can simply put the XPathSelect inside the for-each, this will only be evaluated once.

> How do I make it easier?  Its looping through the prices.

my personal thought when looking at your code was that you wanted to use the repeater to "repeat" through the prices. Now you are doing that in the code behind by hand, making the repeater effectively redundant (unless it is part of a large whole, i.e., if the repeater is used to loop through the products).

I can help you set it up for the repeater to loop through the prices, either here or, if you prefer (since it is technically a new thread/question) on another question.

-- Abel --
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now