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 Articles
- 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
- Optimize LINQ queries
- EF keep throwing error System.InvalidOperationException ASP.NET MVC
- Linq Combine Fields
- Simplifying overly complicated LINQ query - queries
- LINQ + WCF + Transactions
- Return LINQ query to DataTable .NET 3.5?
- How to get sum of hours and minutes in sql
- Linq to get a list of models per Make into DTO
- timezone in Where clause of linq query
- NHibernate delete from LINQ results
- Entity Framework Uninitialised Collection
- How to perform this EF Core nested date comparison query in SQL
- Define "var" type out of a method scope
- Linq to Moq - Nullable Types
- Find an XElement with a certain attribute name and value with LINQ
- Split and select specific elements
- how to practically assign repeating objects from groups
- Log all linq queries executed on DbContext - C#
- Selecting Multiple Rows from DataTable using Linq
- Query on DBcontext using value object returns client evaluation explicitly in EFCore 3.1