Click here to Skip to main content
15,508,473 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
XmlDocument xDoc = new XmlDocument();
        xDoc.Load(Server.MapPath("xmlfilename"));
        XmlNodeList nodes = xDoc.SelectNodes("//track[@id='id']");
        for (int i = nodes.Count - 1; i >= 0; i--)
        {
            nodes[i].ParentNode.RemoveChild(nodes[i]);
        }

        xDoc.Save(Server.MapPath("xmlfilename"));


i debug the above code .if i delete in aspx page (gridview) then it d'not delete the node dynamically in xml file. when i insert the new entery in xml file then only it refresh and show the correct nodes of the xml file
Posted
Updated 8-Nov-14 5:51am
v2
Comments
Swinkaran 9-Nov-14 16:29pm    
Not clear. So, are you loading the XML in a gridview? When you delete a record in gridview, you are expecting the same record to be deleted from the XML and update the gridview. AM I correct?

1 solution

XML
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>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900