private List_ignoreList; private void InitIgnoreList() { _ignoreList = new List { "/configuration/appSettings/add[(@key='CMSTrialKey')]", "/configuration/appSettings/add[(@key='CMSApplicationGuid')]", "/configuration/appSettings/add[(@key='CMSApplicationName')]", "/configuration/connectionStrings", "/configuration/system.web/customErrors", "/configuration/appSettings/add[(@key='LISALastUpdatedVersionTime')]", "/configuration/appSettings/add[(@key='LISAUpdatedVersion')]" }; }
public void RemoveIgnore(XmlDocument doc) { foreach (var xpath in _ignoreList) { var nodeList = doc.SelectNodes(xpath); // apply your xpath here if (nodeList == null) { continue; } foreach (XmlNode node in nodeList) { Console.WriteLine(node.OuterXml); RemoveChildNode(node); } } } private void RemoveChildNode(XmlNode childNode) { var parentNode = childNode.ParentNode; if (parentNode == null) return; parentNode.RemoveChild(childNode); while (parentNode != null && !parentNode.HasChildNodes) { var ancestorNode = parentNode.ParentNode; ancestorNode?.RemoveChild(parentNode); parentNode = ancestorNode; } }