Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am having an xml file ,can i delete one parent node without impacting the child nodes?

The xml is like below
XML
<?xml version="1.0">
<configuration>
<appsetting>
<add key="" value="">
</appsetting>
</configuration>

i need to delete only the configuration tags without hampering other tags ,pls suggest

the output will looks as below.
XML
<?xml version="1.0">
<appsetting>
<add key="" value="">
</appsetting>
Posted
Updated 19-May-15 1:45am
v2
Comments
Sinisa Hajnal 19-May-15 8:03am    
You have to create document fragment with child nodes of the node you want to delete. Add it to deleted node parent (in this case, root node). After that you can delete the node.

1 solution

Almost...
A child node, by definition, can not exists without parent, so what you have to do is to move ALL child nodes of the parent to an other parent and then remove - the now empty - node...
LINQ to XML has a lot of cool methods to do such thing with ease...
One of them is AddBeforeSelf[^]...
C#
XElement nodeToRemove = bla-bla-bla;

nodeToRemove.AddBeforeSelf(nodeToRemove.Elements());
nodeToRemove.Remove();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900