How to remove xml elements using RemoveChild()?

This how to shows how to remove some xml nodes:

XML File:

<?xml version="1.0" encoding="utf-8"?>
<CategoryList>
  <Category ID="01">
    <MainCategory>XML</MainCategory>
    <Description>This is a list my XML articles.</Description>
    <Active>true</Active>
  </Category>
</CategoryList>

Code:



XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("categories.xml"));
 
XmlNode node = xmlDoc.SelectSingleNode("/CategoryList/Category[@ID='01']");
XmlNode commonParent = node.ParentNode;

if(node != null)
{
    commonParent.RemoveChild(node);
}

xmlDoc.Save(Server.MapPath("categories.xml"));

 

I am Muhammod Arifur Rahman, CEO and founder of Learneveryday.net I worked on asp.net mainly. My second skill language is php and wordpress. I am also work on jquery. I love to work with new problem and feel happy when solve. www.smartdatasoft.com is my own company.

Got something to say? Go for it!

*

 
More in Xml (3 of 4 articles)