最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

如何在不删除子节点的情况下从Xml中删除父节点。

SEO心得admin37浏览0评论
本文介绍了如何在不删除子节点的情况下从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();

发布评论

评论列表(0)

  1. 暂无评论