score:2
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}"),
…
}).ToList();
score:1
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),
....
}).ToList();
Source: stackoverflow.com
Related Articles
- Obtain child node based on attribute of parent node
- Get child node attribute value based on parent node attribute value
- Selecting nth child element based on parent elements attribute
- LINQ to XML Select Nodes based on child node and attribute
- Querying xml parent node attribute from child node
- Linq to XML get nested child attribute based on parent attribute
- C# Linq XML Query where multiple elements of same name from a parent node based on a child node value
- Get XML tag attribute value from parent node to last child node
- Remove the Parent Node based on Child condition
- Get the parent node on the basis of single child element with specific attribute
- Select and show only parent node based on child selection
- Auto load child items from immediate parent based on XML attribute
- add new child nodes in sibling descendant node based node's child node attribute value
- Order parent object list by child list attribute
- How to get parent and only one child node
- Linq selecting parent based on child criteria
- How do you sort an XDocument parent node based its child?
- Select Parent XML Elements based on Child element values LINQ C#
- How can I fetch child entities as DTO in parent using reusable queries/Expression's with EF code first?
- most efficient Entity Framework Code First method of flattening / projecting parent entity with specific child
- LINQ to SQL join two tables to select parent table twice based on two different columns from child table
- LINQ - get parent based on last child condition
- Using Lambda / Linq Filter Parent Collection based on Child Items
- Remove child XML node from parent with where clause
- How to select child nodes when parent node equals a specific value in LINQ to XML
- Search and get all child nodes of XML node base on attribute id
- issue removing a XDocument node based on its attribute
- How to add Parent node if Child node elements are equal?
- Find dead nodes from Parent Child Node collection
- Linq to xml select list of nodes based on child attribute
- Use LINQ to get items in one List<>, that are not in another List<>
- How can i get all subnode values separately from XML Response using LINQ to XML?
- LINQ and web service cannot return anonymous types, and you cannot construct an object in a query?
- Max return value if empty query
- Convert LINQ to SQL query
- Join Nhibernate
- Linq query comparing dates - convert string column
- Convert SQL into Linq query
- Count of distinct with linq to entities and custom IEqualityComparer
- Linq Nhibernate : Unable to pull a value from a foreign key table
- Can a class inherit from a collection and still efficiently interact with LINQ?
- Is it possible to have a make a generic method which can take the place of three methods with a System.Data.Linq.Mapping.FunctionAttribute?
- Find child objects in list of parent objects using LINQ
- Multi-dimensional arrays in Linq
- List of common sources and destinations
- Linq to Entities "does not recognize the method ... method, and this method cannot be translated into a store expression."
- Linq2Sql Join Into DefaultIfEmpty
- C# XML Select dictionary node by key value
- Switching DbSet by name
- C# Multiple Linq Query Search Issue