Remove XML Element - CSharp System.Xml

CSharp examples for System.Xml:XML Element

Description

Remove XML Element

Demo Code


using System.Xml.XPath;
using System.Xml;
using System.Text;
using System.IO;/*w w w.java  2  s. c  om*/
using System;

public class Main{
        public static void RemoveElement(XmlElement element)
        {
            var parentNode = element.ParentNode;
            parentNode.RemoveChild(element);
        }
        #endregion

        #region RemoveElement

        public static void RemoveElement(XmlDocument document, string XPath)
        {
            XmlNode parentNode = null;
            XmlNodeList nodes = null;

            nodes = document.SelectNodes(XPath);
            if (nodes != null)
            {
                foreach (XmlElement node in nodes)
                {
                    if (node != null)
                    {
                        parentNode = node.ParentNode;
                        node.RemoveAll();
                        parentNode.RemoveChild(node);
                    }
                }
            }
        }
}

Related Tutorials