score:3
Accepted answer
var document = XDocument.Parse(xml);
var bookElements = document.Descendants("book")
.Where(arg => arg.Element("author").Element("first-name").Value == "Benjamin")
.ToList();
or
var bookElements = document.Descendants("first-name")
.Where(arg => arg.Value == "Benjamin")
.Select(arg => arg.Parent.Parent)
.ToList();
[Edit] As you keep editing the question, I will edit the answer :).
To find the first book that meets the criteria:
var foundBookElement = document.Descendants("book")
.Where(arg => arg.Element("author").Element("first-name").Value == "Benjamin")
.FirstOrDefault();
foundBookElement
will be null if none of the books match the criteria.
Source: stackoverflow.com
Related Articles
- Getting the previous node of a searched keyword in xml
- Code equivalent to the 'let' keyword in chained LINQ extension method calls
- Keep Getting 'The LINQ expression node type 'Invoke' is not supported in LINQ to Entities' Exception
- LINQ Source Code Available
- .NET 4 Code Contracts: "requires unproven: source != null"
- EF code first - getting DynamicProxies instead of objects. Why?
- Getting several node values using LINQ
- Getting current and previous value
- Getting next and previous on a Queryable
- creating Linq to sqlite dbml from DbLinq source code
- How to take value based on previous node from the given xml file
- Getting an XML node using LINQ
- using LINQ getting previous and next element
- Getting 'Data source is an invalid type' when binding Linq query to Gridview
- Getting "Could not find an implementation of the query pattern for source type 'ExcelQueryable<T>'. " Error
- ASP.net Getting Max Date of database Date column How to avoid Null in Date Column C# My code Attached
- source code for LINQ 101 samples
- LINQ, Getting the next and previous record in my table (I'm Confused)
- how can i add source node as child node in project?
- LINQ request for getting the furthest xml child node with an attribute
- List or Array of String Contain specific word in Html Source Code
- Getting multiple attribute values under node
- Getting XML node based on text value, that ends with XML
- Getting all attribute values from node by specifying the ancestor node using LINQ
- c# Linq or code to extract groups from a single list of source data
- Getting InvalidCastException when trying to implement sorting in Entity Framework Code First using Linq
- Getting the Error in my code when framing LINQ
- Getting Value cannot be null. parameter name: source on release but not in debug
- Getting current, previous and next schedule in Linq
- How to load xml code block into existing xml file at a specific node using linq
- How do I pass a parameter from the select list into a function for joining a linq query?
- Remove list items' property from list in c#
- Read last n elements of an XML file with Linq XDocument
- Linq where list contains one of list
- Convert an object array of object arrays to a two dimensional array of object
- Get count of all items in a list that have one of the properties in another list
- Read XML file using Xml.Xpath and Xml.Linq
- Conditional Joins With Linq
- why gives error Expression cannot contain lambda expressions?
- LINQ query for this scenario
- How to add to linq2sql entity set for master detail if I can't explicitly define the detail?
- ASP.NET C# LINQ conditional WHERE clause
- linq ordered subset of another list
- MongoDB C# driver: LINQ filtering on enum serialises enum as int
- This operation would create an incorrectly structured document
- Building a LINQ query based on a JSON array
- LINQ PredicateBuilder not work in my dynamic search
- LINQ: Sum() of distinct Elements
- How do I write a LINQ query that inverts the grouping of a hierarchical data source?
- Linq Cannot implicitity Convert DateTime,Double