Accepted answer

Your code has several problems. First, the thing the compiler is complaining about is, as @MizardX mentioned, that you are using fo.Element("Value") as if it was a sequence. What you probably want is to write let e = fo.Element("Value") (or skip this part completely and directly write select fo.Element("Value").Value).

Another problem is that your XML is using a namespace, but you aren't. This means that you should create a XNamespace object and use it wherever you have element names.

Also, the way your code is written, AircraftType is a sequence of strings. I assume this is not what you wanted.

And seeing that you want to do the same thing for different values of FieldName, you probably want to make this into a method.

With all the problems mentioned above fixed, the code should look something like this:

static readonly XNamespace ns = XNamespace.Get("urn:crystal-reports:schemas");

string GetFieldValue(XElement fs, string fieldName)
    return (from fo in fs.Descendants(ns + "FormattedReportObject")
            where fo.Attribute("FieldName").Value == fieldName
            let e = fo.Element(ns + "Value")
            select e.Value).Single();
var flts = (from fs in xDoc.Descendants(ns + "FormattedSection")
            select new FlightSchedule
                AircraftType = GetFieldValue(fs, "{AIRCRAFT.Type ID}"),


fo.Element("Value") returns an XElement-object. What you want is probably fo.Elements("Value") (note the plural 's').

The error message was complaining that it didn't know how to iterate over the XElement object.

The reason you are not getting any results, is that the XML-file is using namespaces. To find elements outside the default namespace, you need to prefix the namespace before the node name.

I also noticed that you are not using the fos variable, so that loop is unnecessary. fs.Decendants() is already giving you the correct result.

List<FlightSchedule> flts =
    (from fs in xDoc.Descendants("{urn:crystal-reports:schemas}FormattedSection")
     select new FlightSchedule
         AircraftType =
             (from fo in fs.Descendants("{urn:crystal-reports:schemas}FormattedReportObject")
              where fo.Attribute("FieldName").Value == "{AIRCRAFT.Type ID}"
              from e in fo.Elements("{urn:crystal-reports:schemas}Value")
              select e.Value),

Related Articles