Hi Markus
Still don't see the issue - NodeLists are live, so that as the content
changes, the NodeList changes too..., code is getting a bit unwieldy, but it
works.
Nigel
System.Xml.XmlDocument d = new System.Xml.XmlDocument();
d.Load(@"C:\test.xml");
MessageBox.Show(d.OuterXml);
System.Xml.XmlElement root = d.DocumentElement;
System.Xml.XmlNodeList nl = d.GetElementsByTagName("test");
MessageBox.Show(nl.Count.ToString());
foreach (System.Xml.XmlElement el in nl)
{
MessageBox.Show(el.OuterXml);
}
System.Xml.XmlNode n = nl[2];
root.RemoveChild(n);
MessageBox.Show(nl.Count.ToString());
foreach (System.Xml.XmlElement el in nl)
{
MessageBox.Show(el.OuterXml);
}
"Markus Stehle" wrote:
"Nigel Armstrong" <Ni************@discussions.microsoft.com> wrote in
message news:B6**********************************@microsof t.com... Hi Markus
I don't see the problem? The code that you've written won't actually
compile, because of the case of root/Root...but this works. I've used
GetElementsByTagName rather than SelectNodes, but it is otherwise the
same.
Document Before: C:\test.xml
<?xml version="1.0" encoding="utf-8"?>
<root>
<test>0</test>
<test>1</test>
<test>2</test>
<test>3</test>
<test>4</test>
<test>5</test>
</root>
Code:
System.Xml.XmlDocument d = new System.Xml.XmlDocument();
d.Load(@"C:\test.xml");
System.Xml.XmlElement root = d.DocumentElement;
System.Xml.XmlNode n = d.GetElementsByTagName("test")[2];
root.RemoveChild(n);
d.Save(@"C:\test.xml");
Document After:
<?xml version="1.0" encoding="utf-8"?>
<root>
<test>0</test>
<test>1</test>
<test>3</test>
<test>4</test>
<test>5</test>
</root>
Nigel
Hi Nigel,
sorry, I did not describe the problem excact enough. What I want to do is to
remove the node from the nodelist (which is called 'items' in my sample).
Markus