XPath is a selection expression language -- it never modifies the XML document(s) it operates on.
Therefore, in order to obtain the desired new XML document, you need to either use XML DOM (not recommended) or apply an XSLT transformation to the XML document. The latter is the recommended way to go, since XSLT is a language especially designed for tree transformations.
The XSLT transformation itself is extremely simple:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output omit-xml-declaration="yes" indent="yes"/> <xsl:template match="node()|@*"> <xsl:copy> <xsl:apply-templates select="node()|@*"/> </xsl:copy> </xsl:template> <xsl:template match="dvd[not(location/city='london')]"/> </xsl:stylesheet>
Here, you can find a complete code example how to obtain the result of the transformation as an XmlDocument (or if desired, as an XDocument).
Here's an example using LINQ to XML.
//load the document var document = XDocument.Load(@"C:\Development\Website\dvds.xml"); //get all dvd nodes var dvds = document.Descendants().Where(node => node.Name == "dvd"); //get all dvd nodes that have a city node with a value of "london" var londonDVDs = dvds.Where(dvd => dvd.Descendants().Any(child => child.Name == "city" && child.Value == "london"));
- Create a new XMLDocument by filtering an existing document in c# using xpath
- Delete last record/element from existing XML document using LINQ
- Create New Distinct List(of T) from Existing List(of T) Using LINQ
- Using Linq to XML for XPath Style Document Searching
- Create an ObservableCollection by filtering two ObservableCollections of the same type using LINQ and MVVM
- create a parent node of a specific existing node using c# linq
- How to load xml code block into existing xml file at a specific node using linq
- Convert string to int in one line of code using LINQ
- C# - code to order by a property using the property name as a string
- How do I find the text within a div in the source of a web page using C#
- Entity-framework code is slow when using Include() many times
- Using Linq to objects, how to create an empty dictionary of <string, string> easily?
- filtering a list using LINQ
- Can you create a simple 'EqualityComparer<T>' using a lambda expression
- How to create a view using EF code-first POCO
- Why does C# compiler create private DisplayClass when using LINQ method Any() and how can I avoid it?
- Create a list of one object type from a list of another using Linq
- This operation would create an incorrectly structured document
- Filtering lists using LINQ
- How do I create a nested group-by dictionary using LINQ?
- Is there a pattern using Linq to dynamically create a filter?
- create a dictionary using 2 lists using LINQ
- Using LINQ to create an IEnumerable<> of delta values
- Create Items from 3 collections using Linq
- add data to existing xml file using linq
- How can I create a conditional where clause using LINQ
- How to properly search xml document using LINQ C#
- Using Linq to create crosstab results
- Create all possible combinations of items in a list using Linq and C#
- Is it possible to create one dimension array from two using LINQ?
- c# using Linq to sort a list of decimals
- flat list object into view
- Subsonic Single WHERE clause
- Optimising LINQ TO SQL (currently it takes around 2 - 3 minutes) for report module?
- c# LINQ to XML null value exception
- Nested for loop adds to the list double the amount of times its supposed to
- Calling functions from within linq statement
- Proper way to Categorize by a Property
- Read duplicate datarows from database into business objects and distinct them using LINQ
- XML LINQ statement almost giving desired results
- Using ternary operator in lambda where condition
- Old LINQ "Join Into" Expression cannot be Translated in .Net Core 3.1
- Complex GROUP BY using LINQ
- Select new not data binding rad GridView
- LINQ to Entities GroupBy, OrderByDescending, FirstOrDefault when
- want to get data from specific column of azure mobile service table using linq
- LINQ.Where is way slower than LINQ Query, but LINQ Query cannot use on the fly. how to optimize?
- Code keeps eating Memory
- Why is linq query not putting "where" in SQL statement
- Entity Framework - LINQ selection in POCO generic List property