deleteNode =
doc.SelectSingleNode( String.Format( "descendant::Item[@name='{0}']",
"AC1"));
In general, you should be using more lines to write code like this, and you
should be using asserts:
string s = String.Format("descendant::Item[@name='{0}'], "AC1");
XmlNode n = doc.SelectSingleNode(s);
Debug.Assert(n != null);
Debug.Assert(n.Parent == doc.DocumentElement);
doc.DocumentElement.RemoveChild(n);
This will let you step through the code in the debugger and examine the
values of s and n, both of which are questionable. It also makes your code's
implicit assumptions explicit.
I see two obvious problems with this code:
The XPath you're formulating here looks like this:
descendant::Item[@name='AC1']
This will find the first descendant of the context element where the element
name is "Item" and whose "name" attribute is set to "AC1". Since none of the
elements in the instance document you've posted are named "Item", this won't
find anything. The first assert I added to your code would catch this.
If you want to find *any* descendant element whose "name" attribute is set
to "AC1", you'd use
descendant::*[@name='AC1']
Second: you're searching on the descendant axis, but you're deleting a
child. This will throw an exception if the node you find isn't a child of
the document element, e.g.
<foo>
<bar>
<Item name='AC1'/>
</bar>
</foo>
The second assert I added to your code would catch this.
Hope this helps.
--
Robert Rossney