How to delete a few of ML elements?

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

LVL 1
midfdeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

zc2Commented:
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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
midfdeAuthor 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.
0
midfdeAuthor Commented:
See my previous note.
0
CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

midfdeAuthor Commented:
Sorry about my typo: xc2 --> zc2
0
zc2Commented:
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.
0
midfdeAuthor Commented:
>>...as mostly C++ programmer...
"C" - thinking remnant? What if XmlNodeList did not have its indexer?
0
zc2Commented:
Yeah, kind of that. I've never heard a collection item can not be accessed by its index.
0
midfdeAuthor Commented:
I am not sure indexers and their counterpart for(i=0;...) are applicable for some widely used collections. See e.g., LINQ here.:
0
zc2Commented:
Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
XML

From novice to tech pro — start learning today.