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"?>
       <WEIGHT _ID="1000">
     <WEIGHT _ID="2000">
     <WEIGHT _ID="4000">
     <WEIGHT _ID="5000">
     <WEIGHT _ID="6000">

    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


  • 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