tags:

views:

16

answers:

1

Hi All,
I am trying to write all the dropped elements prperties in a xml document to save in the database.When the user delete a dropped element ,i need to remove that element from the Xml doc.

  XmlDocument oXmlDocument = new XmlDocument();
    oXmlDocument.Load(@"D:\VanithaApps\SenMail\DiagramData.xml");
    Boolean nodeExits = false;
    XmlNode oXmlRootNode = oXmlDocument.SelectSingleNode("records");      
    XmlNodeList xmlnode = oXmlDocument.GetElementsByTagName("record");

    if (delete=="1")
    {
        if (xmlnode.Count > 0)
        {
            for (int i = 0; i < xmlnode.Count; i++)
            {
                string tempVar = element.Substring(0, element.Length - 1);
                if (xmlnode[i].ChildNodes[2].InnerText == tempVar)
                {
                    try
                    {
                        oXmlRootNode.RemoveChild(xmlnode[i]);
                        goto Found;
                    }
                    catch(Exception ex)
                    {
                        ex.ToString();
                    }

                }
            }
        }
    }

    if (xmlnode.Count > 0)
    {
        for (int i = 0; i < xmlnode.Count; i++)
        {
            string tempVar = element.Substring(0, element.Length-1);
            if (xmlnode[i].ChildNodes[2].InnerText == tempVar)
            {
                nodeExits = true;
                XmlNode XAxis = xmlnode[i].ChildNodes[0];
                XAxis.InnerText = Convert.ToString(x);

                XmlNode YAxis = xmlnode[i].ChildNodes[1];
                YAxis.InnerText = Convert.ToString(y);
            }

        }
         if (nodeExits == false)
            {
                CreateNewNode(x, y, element, userid, oXmlDocument, oXmlRootNode);

            }
    }

    else
    {
        CreateNewNode(x, y, element, userid, oXmlDocument, oXmlRootNode);
    }

    Found:

    int result = 0;
    return result;

I haven't used xml extensively before
<?xml version="1.0" encoding="utf-8"?> <records> <record> <X-Cordinate>774</X-Cordinate> <Y-Cordinate>173</Y-Cordinate> <Element>drag595</Element> <UserID>1</UserID> </record> </records>
i want to delete whose child 'Element' value is equal to tempVar.Here if the Element value is equal to drag595 ,i want to remove that entry from my XML .

A: 

Change

oXmlRootNode.RemoveChild(xmlnode[i]);//not working//

to

xmlnode[i].ParentNode.RemoveChild(xmlnode[i]);

Children can only be removed from their respective parents.

Tomalak
@Tomalak I tried the xmlnode[i].ParentNode.RemoveChild(xmlnode[i]); and it does nothing ,that's y i moved to .Removechild.PLease see my full edited code block above.
Vani