Wednesday, January 4, 2012

How to remove Node from XML File?


Removing any single node from XML file is an easy task. But when we go for delete more than one node (also Child Node) from XML file, based on some condition then you get problem. Generally we use looping to remove node from XML file, but the main problem is that-
 

After deleting one node, control comes out from loop, means even if we have written code to delete more than one XML node, it comes out from loop. 

Workaround-

To solve this problem, make recursive call to delete XML Node.

In this article, I am going to explain you- How you can overcome this problem?

Here I have written following function to solve this problem-

 //Recursive function to remove XML node with InnerText StrRemoveValue
        private void RemoveXMLNode(XmlNodeList MainParentNode, string StrRemoveValue)
        {
            try
            {
                for (int i = 0; i < MainParentNode.Count; i++)
                {
                    XmlNode node = MainParentNode[i];
                    //Remove XML node if it's InnerText is StrRemoveValue
                    if (node.InnerText == StrRemoveValue)
                    {
                        //getting parent of current node
                        XmlNode parentNode = node.ParentNode;
                        //Deleting node from parent node
                        parentNode.RemoveChild(node);

                        //Again calling RemoveXMLNode() to delete other xml node with InnerText is equal to StrRemoveValue

                        RemoveXMLNode(parentNode.ChildNodes, StrRemoveValue);
                    }
                    else
                    {
                        //Calling function for child nodes
                        RemoveXMLNode(node.ChildNodes, StrRemoveValue);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }


In this function this is main line

    RemoveXMLNode(parentNode.ChildNodes, StrRemoveValue);

Here I am calling function recursively, because after using this line control doesn't come out from loop and it process code to remove other child nodes. I have called this function in button_Click() event like this- 


RemoveXMLNode(RootList,"15000"); 

Here I am deleting XML node based on condition - If InnerText of any node is '15000' then delete that node.

Here is code-

 private void btnRemove_Click(object sender, EventArgs e)
        {
            try
            {
                XmlDocument xmlDoc = new XmlDocument();
              
                //Loading XML file
                xmlDoc.Load(@"c:\myxml.xml");
              
                //Getting root node from XML
                XmlNodeList RootList = xmlDoc.GetElementsByTagName("Emp");
              
                //Calling function to delete node
                //If any node has InnerText 15000 then that will be deleted
                RemoveXMLNode(RootList,"15000");
              
                //Again saving XML file
                xmlDoc.Save(@"c:\myxmlNew.xml");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }


Now done, after execution of this code modified XML file will be saved in this location-

c:\myxmlNew.xml


Thanks

No comments:

Post a Comment