如何在不删除子节点的情况下从Xml中删除父节点。 [英] How Can I Delete A Parent Node From An Xml Without Deleting The Child Node.

查看:299
本文介绍了如何在不删除子节点的情况下从Xml中删除父节点。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个xml文件,我可以删除一个父节点而不影响子节点吗?



xml如下所示

I am having an xml file ,can i delete one parent node without impacting the child nodes?

The xml is like below

<?xml version="1.0">
<configuration>
<appsetting>
<add key="" value="">
</appsetting>
</configuration>



i只需要删除配置标签没有妨碍其他标签,请建议



输出将如下所示。


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

the output will looks as below.

<?xml version="1.0">
<appsetting>
<add key="" value="">
</appsetting>

推荐答案

差不多。 ..

根据定义,子节点在没有父节点的情况下不能存在,因此您需要做的是将父节点的所有子节点移动到另一个父节点然后删除 - 现在为空 - 节点...

LINQ to XML有很多很酷的方法o轻松做到这一点...

其中一个是 AddBeforeSelf [ ^ ] ...

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[^]...
XElement nodeToRemove = bla-bla-bla;

nodeToRemove.AddBeforeSelf(nodeToRemove.Elements());
nodeToRemove.Remove();


这篇关于如何在不删除子节点的情况下从Xml中删除父节点。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆