function counts wrongly

  • Thread starter Thread starter Frank Zimmermann
  • Start date Start date
F

Frank Zimmermann

I write a C# program (VS.NET 2003) for my PocketPC. This program use a
XML-File.
I count the element in the xml-file which these code piece.

....
XmlDocument store = new XmlDocument();
store.Load ("/Program Files/Test.xml");

int Anzahl = store.DocumentElement.ChildNodes.Count;
....
In a other part of the programm i delete a element from the xml-file and i
save the file with store.Save(...)
When i count the element after the delete-operation i became the old count.
For example: before deleting a element Anzahl = 10
after Anzahl = 10 but the the file has now 9 elements...

What is wrong?




Mail: (e-mail address removed)
 
ok

XmlDocument store = new XmlDocument();
store.Load ("/Program Files/Test.xml");

int Anzahl = store.DocumentElement.ChildNodes.Count;
....//for example Anzahl = 10
//Delete a element
....
store.DocumentElement.ChildNodes[id].RemoveAll();


store.Save("/Program Files/Test.xml");

...
after delete is the xml file ok also without the one element
....
//check count

XmlDocument store = new XmlDocument();
store.Load ("/Program Files/Test.xml");

int Anzahl = store.DocumentElement.ChildNodes.Count;

//here is the Anzahl = 10 , too ! but they must be 9
What is wrong?
 
store.DocumentElement.ChildNodes[id].RemoveAll();
RemoveAll() removes just the attributes and child nodes (if any) of this
node but not the node itself. So that's why the count is still 10.
what you want to do in order to remove that node is
store.DocumentElement.RemoveChild(store.DocumentElement.ChildNodes[id]);
After that the new count will be 9.

HTH
Cheers.


This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top