When I output this code, I get { Price: 1446 } when I only want the number without the braces. Is there a way to do this? Also, once I get the price value, I want to convert it to a decimal. Is there a way to do this?
var flightPrice = (from f in XElement.Load(MapPath("flightdata.xml")).Elements("flight")
where (string)f.Element("flightnumber") == FlightNumber
&& (string)f.Element("destinationairportsymbol") == Destination
select new { Price = (Int32)f.Element("price") }
).SingleOrDefault();
lblPrice.Text = flightPrice.ToString();
Your
selectis creating an anonymous type with a single property: Price. If all you want is the actual price (and as a float), change your select toHowever, it is not recommended that you use a
floatto deal with something financial like a price. Thedecimaldata type is the preferred type for such a value.