且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用c#在xml中的现有节点周围添加父节点

更新时间:2023-11-23 22:12:58

请根据您的要求尝试以下代码...



Please try below code for your requirement...

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();
}