开发者

Remove XML Nodes as per user input

开发者 https://www.devze.com 2023-01-18 11:48 出处:网络
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.

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].ChildNo开发者_C百科des[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 .


Change

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

to

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

Children can only be removed from their respective parents.

0

精彩评论

暂无评论...
验证码 换一张
取 消