function counts wrongly

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)
 
F

Frank Zimmermann

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?
 
M

Mark Ihimoyan [MSFT]

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top