Jump to content

Delete XML Parent Node

- - - - -

  • Please log in to reply
No replies to this topic

#1
daydr3am3r

daydr3am3r

    Newbie

  • Members
  • Pip
  • 1 posts
Hi
I'm trying to delete a parent node from this XML file

<?xml version="1.0" encoding="utf-8"?>

<AIO>

  <app>

    <Name>YahooMessenger.exe</Name>

    <Path>C:\Program Files (x86)\Yahoo!\Messenger\YahooMessenger.exe</Path>

  </app>

  <app>

    <Name>msnmsgr.exe</Name>

    <Path>C:\Program Files (x86)\Windows Live\Messenger\msnmsgr.exe</Path>

  </app>

  <app>

    <Name>Skype.exe</Name>

    <Path>C:\Program Files (x86)\Skype\Phone\Skype.exe</Path>

  </app>

  <app>

    <Name>googletalk.exe</Name>

    <Path>C:\Program Files (x86)\Google\Google Talk\googletalk.exe</Path>

  </app>

  <app>

    <Name>OUTLOOK.EXE</Name>

    <Path>C:\Program Files (x86)\Microsoft Office\Office12\OUTLOOK.EXE</Path>

  </app>

  <app>

    <Name>winamp.exe</Name>

    <Path>C:\Program Files (x86)\Winamp\winamp.exe</Path>

  </app>

  <app>

    <Name>Steam.exe</Name>

    <Path>D:\Steam\Steam.exe</Path>

  </app>

  <app>

    <Name>uTorrent.exe</Name>

    <Path>C:\Program Files (x86)\uTorrent\uTorrent.exe</Path>

  </app>

</AIO>

I have a list view in my app and when I select a row and click remove I want it to delete the parent node, in this case APP.
The problem is that I use "Name" to identify the child node.
I tried this to delete the child node

public void DelApp()

        {

            int ID = int.Parse(lvApps.SelectedItems[0].SubItems[1].Text.ToString());

            string del_querry = lvApps.SelectedItems[0].SubItems[2].Text.ToString();


            XmlDocument AIO = new XmlDocument();


            if (File.Exists(filename))

            {

                AIO.Load(filename);


                XmlElement elmRoot = AIO.DocumentElement;


                XmlNodeList lstApps = AIO.GetElementsByTagName("app");


                foreach (XmlNode node in lstApps)

                {

                    XmlNodeList lstChildren = node.ChildNodes;


                    foreach (XmlNode name in lstChildren)

                    {

                        if (name.InnerText == del_querry)

                        {

                            node.RemoveChild(name);

                            AIO.Save(filename);

                            break;

                        }

                    }

                }

            }

        }

Also, XmlNode.RemoveAll();
works fine but it deletes only the child nodes leaving the empty <app></app>.
Is it possible to delete the parent node using a child node to identify it or I am doing it the wrong way?
I noticed that in some cases the XML file is indexed, something like this <app id=1>.
I also have an idea on how to delete the app node using XmlNodeList.Count Property (System.Xml) but I haven't tried that yet. I first wanna know if I'm on the right track or not.
I also used something like this to delete the parent node but the main problem is how do I identify the parent node.

XmlNode localnode = AIO.SelectSingleNode("/AIO/app");

localnode.ParentNode.RemoveChild(localnode);

AIO.Save(filename);






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users