1

I'm pretty new to using LINQ so I'm not entirely sure of all the correct syntaxes etc.

Here's what I've got so far

My XML

<?xml version="1.0"?>
<Bookings>
    <Booking BookingNumber="300067649">
        <FLIGHTS>
            <FlightGroups>
                <FlightGroup ID="1 ">
                    <Flights>
                        <Flight ID="1">
                            <ADULTS>1</ADULTS>
                            <DEPARTURE_DATE>18/02/2006</DEPARTURE_DATE>
                        </Flight>
                    </Flights>
                </FlightGroup>
            </FlightGroups>
        </FLIGHTS>
    </Booking>
</Bookings>

I get this from a web service controlled by an outside company so i'm unable to change the syntax

Here's the LINQ i'm using in my .Net code

Dim root As XElement = XElement.Load(New StringReader(xmlstring))
Dim tests As IEnumerable(Of XElement) = From el In
    root.Elements("Booking").Elements("FLIGHTS").Descendants() 
    Where CType(el.Element("DEPARTURE_DATE"), DateTime).Date <= DateTime.Now.Date

For Each el As XElement In tests
    Response.Write(el)
Next

The string xmlstring being the XML from above.

The problem is that this is giving me an error which is:

Value cannot be null.
Parameter name: element

Basically i'm trying to select all flights that are before todays date and just can't seem to get this to work.

Any help would be appreciated

1
  • 2
    FYI, you can use XElement.Parse() instead of Load when loading a String. Commented Feb 28, 2012 at 14:11

1 Answer 1

1

Depending on your current culture, 18/02/2006 may not be convertible to a DateTime (the day comes before the month. If you're in the US, the month should come before the day).

This throws an error (I'm in the USA).

var d1 = DateTime.Parse("18/02/2006");

This works:

var d2 = DateTime.Parse("02/18/2006");

I just tried the test below, and everything worked as expected (I changed the format of the date). You may have to provide a format provider for converting the date.

var str = @"<?xml version=""1.0""?>
<Bookings>
    <Booking BookingNumber=""300067649"">
        <FLIGHTS>
            <FlightGroups>
                <FlightGroup ID=""1"">
                    <Flights>
                        <Flight ID=""1"">
                            <ADULTS>1</ADULTS>
                            <DEPARTURE_DATE>02/18/2006</DEPARTURE_DATE>
                        </Flight>
                    </Flights>
                </FlightGroup>
            </FlightGroups>
        </FLIGHTS>
    </Booking>
</Bookings>
";
var xel = System.Xml.Linq.XElement.Parse(str);

var flights = xel.Descendants("Flight");
var tests = flights.Where(f => DateTime.Parse(f.Element("DEPARTURE_DATE").Value).Date < DateTime.Now.Date);
foreach (var test in tests)
{
    System.Console.WriteLine(test.Element("DEPARTURE_DATE").Value);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks - i've got this to work - I converted your working code to VB and the syntax is different in the WHERE part of the statement

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.