score:1
Accepted answer
your first line doesn't do anything if you don't actually use the results.
foreach (var outputelement in rootelement.elements("output")
.where(e => (string)e.attribute("id") == "1"))
{
foreach (var pathelement in outputelement.elements("path"))
{
// ...
}
}
if your id
attribute is guaranteed to be unique (which it should), you can get rid of the first foreach and just get the individual <output>
directly:
var outputelement = rootelement.elements("output")
.firstordefault(e => (string)e.attribute("id") == "1"));
score:0
i would recommend using some xpath to select nodes you need:
foreach (xelement path in root.xpathselectelements("/output/path[../@id=1]"))
{
string value = path.value;
}
indeed, sometimes xpath lets you write more readable and maintanable code. you just use some expression that replaces several linq-to-xml statements.
Source: stackoverflow.com
Related Query
- Linq query based on attribute
- LINQ Query to return list based on attribute of DB field
- Filtering out duplicate XElements based on an attribute value from a Linq query
- LINQ query to perform a projection, skipping or wrapping exceptions where source throws on IEnumerable.GetNext()
- Splitting LINQ query based on predicate
- How to query xsi:type from an attribute using Linq to XML?
- Is there any way to create a LINQ query as a variable without having the data source (yet)?
- LINQ Source Code Available
- Linq query - find strings based upon first letter b/w two ranges
- how to take 100 records from linq query based on a condition
- how do I write LINQ query to retrieve distinct records based on only specific properties?
- LINQ query on an object list to get distribution based on multiple fields
- filter a linq query based on the results of another query's results
- linq - how do you do a query for items in one query source that are not in another one?
- How can I write the following code more elegantly using LINQ query syntax?
- How to dynamic add filters to a LINQ query against an Odata Source in C#
- Exception in a CRM LINQ query with joins. Attribute in second table doesn't exist
- C# Linq query help removing foreach loops creating cleaner code
- Use a linq query as microsoft local report Data Source (WinForms)
- Determine the source DataContext for a Linq to Sql query
- LINQ query returns old results when source list is re-initialized
- LINQ query to filter based on Substring of a list
- How to get SQL query into LINQ form in C# code
- Linq to query results of list of objects based on another list
- How can I code a Linq query to do an upward Include?
- Linq query to find duplicate objects based on multiple fields AND property is null
- Creating dynamic Linq query based on property values
- creating Linq to sqlite dbml from DbLinq source code
- Re-using LINQ query based on bool value
- Identify source of linq to sql query
More Query from same tag
- Dynamic linq query not working
- Returning multiple fields with LINQ GroupBy on a datatable
- Showing First Instance and Last Instance of GroupBy ASP.NET MVC
- Linq to Entity : add filter to child entities of child entities
- Coalesce in Dynamic Linq
- Order by repeated times LINQ ASP.NET MVC
- Linq Return Type
- Getting sublist from a list in Linq
- How do I return single data from primary table and multiple entries in second table nested after join
- MongoDB push to nested array using Linq expressions
- Linq grouping into list
- LINQ IsAssignableFrom check between two Type[]?
- Use linq to populate model with data from two diffrent models
- c# check if datagridview column contains value & linq First() vs FirstOrDefault()
- Optimize IQueryable query to let EF generate a single SQL query instead multiple. Child collection of an entity must contains a custom collection
- Removing from a collection while in a foreach with linq
- Query table including selfreference childs
- How to use LINQ to adjust result?
- How to convert an IQueryable<T> to a List<T>?
- Am I lazy loading these properties?
- How to restore the quantity and consumption fields to their original numbers in the Item table after deleting an Order with Items
- LINQ Datatable and Select columns
- How to convert Infrastructure.DbQuery to ViewModel type?
- C# Linq to entity (EF Core) create SQL statement "like '1 %'" : Startswith doesn't work when passed a variable
- $.ajax success: value is always undefined
- Concatenate Datetime and string and bind result to dropdownlist using Json method
- PredicateBuilder Where List inside List with C#
- ToListAsync throws 'Value cannot be null.' Exception
- Is it possible to write this as a linq query?
- LINQ Operations on Lambda Grouping