Delete XML Node using C#

By Gohil Jayendrasinh Nov 29, 2012
This blog defines how to delete XML node in C#.
Download Files: DeleteNodefromXML.ra

Read the xml file.

This function is use for delete xml node dynamically.

    /// <summary>
    ///

    ///
</summary>
    /// <param name="path">xml file path
</param>
    /// <param name="tagname">root tagname
</param>
    /// <param name="searchconditionAttributename">Search Attribute Name
</param>
    /// <param name="searchconditionAttributevalue">Search Attribute Value
</param>

    private static void DeleteXmlNode(string path, string tagname, string searchconditionAttributename, string searchconditionAttributevalue)
    {
        XmlDocument doc = new XmlDocument();
        doc.Load(path);
        XmlNodeList nodes = doc.GetElementsByTagName(tagname);\
      
//XmlNodeList nodes = doc.GetElementsByTagName("user");

        foreach (XmlNode node in nodes)
        {

            foreach (XmlAttribute attribute in node.Attributes)

            {

                if ((attribute.Name == searchconditionAttributename) && (attribute.Value == searchconditionAttributevalue))

               
//if ((attribute.Name == "name") && (attribute.Value == "aaa"))

                {

                   
//delete.

                    node.RemoveAll();

                    break;

                }

            }

        }

       
//save xml file.

        doc.Save(path);

    }
 

This function is use for edit node in xml.

  /// <summary>
    ///

    ///
</summary>
    /// <param name="path">xml file path
</param>
    /// <param name="tagname">root tagname
</param>
    /// <param name="searchconditionAttributename">Search Attribute Name
</param>
    /// <param name="searchconditionAttributevalue">Search Attribute Value
</param>
    /// <param name="editAttributename">Edit Attribute Name
</param>
    /// <param name="editAttributevalue">Edit Attribute Value
</param>

    private static void EditXmlNode(string path, string tagname, string searchconditionAttributename, string searchconditionAttributevalue, string editAttributename, string editAttributevalue)
    {
        XmlDocument doc = new XmlDocument();
        doc.Load(path);

        XmlNodeList nodes = doc.GetElementsByTagName(tagname);
       
// XmlNodeList nodes = doc.GetElementsByTagName("user");

        foreach (XmlNode node in nodes)

        {

            bool newnode = true;

           
//Check attribute eixt which we want to edit.

            bool isEdit = false;

            foreach (XmlAttribute attribute in node.Attributes)

            {

               
//if ((attribute.Name == "name") && (attribute.Value == "ddd"))

                if ((attribute.Name == searchconditionAttributename) && (attribute.Value == searchconditionAttributevalue))

                {

                    isEdit = true;

                }

                if (isEdit)

                {

                   
// if (attribute.Name == "expireson")

                    if (attribute.Name == editAttributename)

                    {

                       
//attribute.Value = "2011-12-28";

                        attribute.Value = editAttributevalue;

                        break;

                    }

                }

            }

            newnode = false;

        }

       
//save xml file.

        doc.Save(path);

    }

I use following .xml file

<?xml version="1.0" encoding="utf-8"?>
<
users>
  <
user />
  <
user name='aaa' password='111' status='0' expireson='2012-12-31' createdon='2011-05-31' />
  <
user name='bbb' password='222' status='0' expireson='2012-12-31' createdon='2011-05-31'/>
  <
user name='ccc' password='333' status='0' expireson='2011-12-31' createdon='2011-05-31'/>
  <
user name='ddd' password='444' status='0' expireson='2011-12-31' createdon='2011-05-31'/>
</
users>

Gohil Jayendrasinh

  6 + years’ experience in the Field of .NET. MCTS in ASP.NET 3.5MS Dynamic NAV Web Services, ASP.NET,Windows Form Application,WCF,Web Services,SQL SERVER 2005/2008 ,SSRS ,AJAX,javaScript,jQuery

COMMENT USING

PREMIUM SPONSORS

Hire Mobile & Web Developer on demand. 100% satisfaction. Try for 1 week or Money Back. Local and remote developers available all over USA.

Latest Blogs