locked
I have xml file which consist weight and price...how can i query the xml file using select case statements? RRS feed

  • Question

  • User767034699 posted

    here is my xml file

    <?xml version="1.0" encoding="utf-8"?>
    <MAIN>
       <WEIGHT _ID="1000">
        <AMOUNT>42.20</AMOUNT>
      </WEIGHT>
     <WEIGHT _ID="2000">
        <AMOUNT>49.20.00</AMOUNT>
      </PRICE>
     <WEIGHT_ID="3000">
        <AMOUNT>54.20</AMOUNT>
      </WEIGHT>
     <WEIGHT _ID="4000">
        <AMOUNT>59.20</AMOUNT>
      </WEIGHT>
     <WEIGHT _ID="5000">
        <AMOUNT>64.20</AMOUNT>
      </WEIGHT>
     <WEIGHT _ID="6000">
        <AMOUNT>69.20</AMOUNT>
      </WEIGHT>
    </MAIN>

    if user enters a weight for example 900 it should only show 42.20 amount

    below is my code

      <WebMethod()> _
        Public Function Get_Price(ByVal weight As String) As String
            Dim price As String
            Dim xml As XElement = XElement.Load("~\pricing.xml")
            Dim query1 = (From d In xml.Elements()
                                     Where CStr((d.Attribute("_ID"))) <= weight
                                       Select d).FirstOrDefault
    
    
            price = query1.Element("AMOUNT").Value.ToString()
    
    
            Return price
        End Function

    for some reason it returns the wrong price

    Thursday, August 7, 2014 2:58 AM

Answers

  • User-1638594242 posted

    Try this

    var query = (from d in xml.Elements()
    where Convert.ToInt32((d.Attribute("_ID").Value))> Convert.ToInt32(weight)
    select d).FirstOrDefault();

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 7, 2014 1:12 PM