本文介绍了如何使用c#在xml中的现有节点周围添加父节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个appsetting文件如下。

I am having a appsetting file as below.

<appsettings>
<add key="test1" value="chk1" />
<add key="test2" value="chk2" />
<add key="test3" value="chk3" />
</appsettings>





但appsetting文件中缺少配置部分,因为它是从we​​b.config文件中引用的。



我想将上面的appsetting文件读作xml,并在appsetting节点上添加一个< configuration> 来从外部应用程序中读取密钥。



阅读后我想删除那个配置节点

可以帮我个人添加和删除配置节点arround appsetting节点吗?



but the configuration section is missing in the appsetting file as it has been refered from the web.config file.

I want to read the above appsetting file as xml and add a <configuration> arround the appsetting node to read the keys from an external application.

After reading i want to remove that configuraion node
can any body help me out to add and remove the configuration node arround appsetting node ?

推荐答案

XDocument xmlDoc = XDocument.Load(filePath);
bool isExists = (from data in xmlDoc.Element("appsettings")
				select data).Any();
if (!isExists)
{
	    XmlElement parent = xmlDoc.CreateElement("configuration");
            //Now find that appSettings node and append it to parent Element
}

//Delete Node
XmlNodeList newXMLNodes = xmlDoc.SelectNodes("/configuration/appsettings");
foreach (XmlNode child in newXMLNodes)
{
  child.ParentNode.RemoveAll();
}


这篇关于如何使用c#在xml中的现有节点周围添加父节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 21:02