How to delete a few of ML elements?

midfde
midfde used Ask the Experts™
on
What is the best way of deleting unknown number of XML elements with the same tag, please.
Please see the code and its result for the context of the question:
Source code:
class Program {
        static void Main(string[] args) {
            XmlDocument dom = new XmlDocument();
            dom.LoadXml(@"<A><a /><a /><a /><a /></A>");
            foreach (XmlElement x in dom.GetElementsByTagName("a")){
                Console.WriteLine(x.Name);
            }
            try{
                foreach (XmlElement x in dom.GetElementsByTagName("a")){
                    Console.WriteLine("--" + x.Name);
                    x.ParentNode.RemoveChild(x); 
                }
            } catch (Exception e) { Console.WriteLine("Error::" + e.Message); }
            Console.Read();
        }
    }

Open in new window

Output on the console is as follows:
a
a
a
a
--a
Error::The element list has changed. The enumeration operation failed to continue.

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Try not to use "for each", but enumerate the items in the collection from the end to the begining.

                XmlNodeList a_elems = dom.GetElementsByTagName("a");
                for(int i = a_elems.Count - 1; i >= 0; i--) {
                    XmlNode x = a_elems[i];
                    Console.WriteLine( "--" + x.Name );
                    x.ParentNode.RemoveChild( x );
                }

Open in new window

Author

Commented:
Thank you, xc2.
Your code does what I want, but...
For me it is not the best becuse I dislike two of its fetures:
1) It introduces unnecessary enumeration (I want to say: "delete all that..." rather than delete all with numbers ..." (met in my solution)
2) I'd like to somehow avoid using of the parent; I'd like to say "Delete this", rather than "Delete the child of the parent of this" (still to be met).
Anyway, your solution in a row with mine are illustrated in the attached iamge.The two solutions illustrated.

Author

Commented:
See my previous note.
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

Author

Commented:
Sorry about my typo: xc2 --> zc2
I (as mostly C++ programmer) don't see any much difference between for() and foreach() and why for() could be less desired. But your solution is really working. That's weird, but those two lists created different ways behave different.
There's, AFAIK, no way to delete a node except ask the parent to do that.

Author

Commented:
>>...as mostly C++ programmer...
"C" - thinking remnant? What if XmlNodeList did not have its indexer?
Yeah, kind of that. I've never heard a collection item can not be accessed by its index.

Author

Commented:
I am not sure indexers and their counterpart for(i=0;...) are applicable for some widely used collections. See e.g., LINQ here.:
Thanks

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial