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 Query
- 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
More Query from same tag
- Is there a way to create a list of property values from a list of some type?
- C# Filter Listbox From ComboBox Selection Using LinQ
- Closest value in DataTable
- Using Linq, how to separate a list in to grouped objects by name?
- Linq to filter result from database
- C# + LINQ - inner join
- Linq-to-SQL strange behaviour
- How to remove consecutive duplicate items from a list
- LINQ not trivial Group By
- Update Datatable in c# using linq by joining dictionary
- How do I implement a dynamic 'where' clause in LINQ?
- Can you create a grouping based on ranges?
- Silverlight Observable Collection problem
- `SELECT MIN(ZoneMin), MAX(ZoneMin) FROM Plant` as LINQ to SQL
- Parsing inner double Dictionary using LINQ?
- Linq: Selecting current month Data but checking if anything needs carrying over from previous months
- Split an IEnumerable<T> into fixed-sized chunks (return an IEnumerable<IEnumerable<T>> where the inner sequences are of fixed length)
- Asp.net MVC 3 Linq DateTime
- Getting COUNT and SKIP TAKE in one operation with Linq to Entities
- Comparing two lists and removing each item that contains an entry from the other list
- Why an error occurs after I modified my model class?
- Filtering a UserDefinedFunctionCollection using LINQ?
- Does Any() stop on success?
- SelectListItem and linq any and select statement
- LINQ's Distinct() on a particular property
- How to filter an object list based on an unknown property
- Efficient way to merge 2 xmls into a new XML in LINQ
- Can a LINQ extension method create a new KeyValuePair with a new() .Value when a Where clause isn't satisfied
- How to properly implement a generic method?
- LINQ to SQL: Change timestamp forcefully of a table