1 Replies - 356 Views - Last Post: 01 April 2013 - 02:36 PM Rate Topic: -----

#1 IYIaster  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 06-July 11

Deleting XML Element not working?

Posted 01 April 2013 - 01:59 PM

XML Document

<?xml version="1.0" encoding="utf-8"?>
<computers>
  <computer>
    <name>computer1</name>
  </computer>
  <computer>
    <name>computer2</name>
  </computer>
  <computer>
    <name>computer3</name>
  </computer>
  <computer>
    <name>computer4</name>
  </computer>
  <computer>
    <name>computer5</name>
  </computer>
  <computer>
    <name>computer6</name>
  </computer>
  <computer>
    <name>computer7</name>
  </computer>
  <computer>
    <name>computer8</name>
  </computer>
</computers>



C# script to delete node:


string complist = "D:\\computerlist.xml";
            XmlDocument doc = new XmlDocument();
            doc.Load(complist);
            XmlNodeList nodes = doc.SelectNodes("//computers/computer[@name='" + comboBox1.Text + "']");
            for (int i = nodes.Count - 1; i >= 0; i--)
            {
                nodes[i].ParentNode.RemoveChild(nodes[i]);
            }
            doc.Save(complist);
        }




Thanks for any help!

Is This A Good Question/Topic? 0
  • +

Replies To: Deleting XML Element not working?

#2 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3741
  • View blog
  • Posts: 13,086
  • Joined: 12-December 12

Re: Deleting XML Element not working?

Posted 01 April 2013 - 02:36 PM

The following works for me:

XmlNodeList nodes = doc.SelectNodes("//computer[name='" + "computer5" + "']");

I believe it is because '//' will find nodes wherever they are in the document; in which case, it is not used as a means of navigating further node-branches. I also dropped the @ symbol as it is not used in this way.

That is,
computers/computer[name='computer5']

would work, but

//something/anything_else

will not work.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1