remove an element from an XML

user2697903

I want to remove an element from an XML Tree and I tried using the method mentioned in the following URL to do that.

https://msdn.microsoft.com/en-us/library/bb387051.aspx

My XML is kinda different from the above and when I tried to use this it only removes the Grandchild1 in the first node. It doesn't do it for the second one.

XElement root = XElement.Parse(@"<Root>
    <Child1>
        <GrandChild1/>
        <GrandChild2/>
        <GrandChild3/>
    </Child1>
    <Child1>
        <GrandChild1/>
        <GrandChild2/>
        <GrandChild3/>
    </Child1>
</Root>");

root.Element("Child1").Element("GrandChild1").Remove();

output after executing:

<Child1>       
    <GrandChild2/>
    <GrandChild3/>
</Child1>
<Child1>
    <GrandChild1/>
    <GrandChild2/>
    <GrandChild3/>
</Child1>

expected output:

    <Child1>
        <GrandChild2/>
        <GrandChild3/>
    </Child1>
    <Child1>
        <GrandChild2/>
        <GrandChild3/>
    </Child1>

Why doesn't it do it and how can I make it work?

Thanks!

Jon Skeet

The Element method only returns a single element. You want:

root.Elements("Child1").Elements("GrandChild1").Remove();

That uses:

So that will find every GrandChild1 element directly under a Child1 element directly under the root. If you actually don't care where the GrandChild1 element is found, you an use

root.Descendants("GrandChild1").Remove();

... but using Elements gives you a bit more control.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to remove element from XML

From Dev

XML: How to remove an element from IEnumerable object?

From Dev

Remove XML namespace from child element

From Dev

java xpath remove element from xml

From Dev

Remove space from the xml tag element names

From Dev

Remove Row Element in FOR XML

From Dev

How to remove xmlns attribute from the root element in xml and java

From Dev

How to remove a specific element from XML, using XSLT

From Dev

How do I remove an Element from an XML in C#

From Dev

How to remove element from xml file using text content in java?

From Dev

How do I remove an element and its content from an XML file

From Dev

How to remove unnecessary attributes from element in XML using XSLT?

From Dev

How do I remove all attributes from one XML element?

From Dev

How to remove one specific namespace from XML element

From Dev

Can't remove element and child element from an XML document by Java code

From Dev

Can't remove an element in XML

From Dev

Remove specific XML element in Delphi

From Dev

Remove element in a XML file with Python

From Dev

Remove an element from a vector

From Dev

Remove an Element From HashTable?

From Dev

Remove element from an array

From Dev

Remove an element from Jquery

From Dev

Remove element from HashSet

From Dev

Remove an Element From HashTable?

From Dev

Remove element from an array

From Dev

Remove CDATA from XML

From Java

How can i remove an element or attribute from a json output while parsing from xml, using java

From Dev

extracting element from xml

From Dev

XSLT Remove all attribute for one root element from xml using XSLT