我有一个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();