I have an XML document that I'm looping through and modifying as I go.
Some nodes have to be deleted and I don't want to process their
children after deleting the node.
If my XML structure looks like this:
<top>
<elem>
<elem>first level2</elem>
</elem>
<elem deleteMe="true">
<elem>second level 2</elem>
</elem>
</top>
and the code looks like this:
XmlNodeList nodes = doc.Select("top//elem");
foreach(XmlNode n in nodes)
DoSomething(n);
Say that "DoSomething()" will delete the node that has the "deleteMe"
attribute.
The affect that I'm looking for is that after you delete the <elem
deleteMe="true"> element, you won't process the <elem> inside it (the
one with the inner text of "second level 2"). However, the loop still
processes that element. I'm guessing it's because the XmlNodeList still
has a reference to it. If I requery using the Select() method, the
deleted node and all its children will be excluded. The problem is that
I can't just requery in the middle of doing everything.
I can think of a few somewhat nasty ways to solve this problem, but I
was hoping there would be a clean approach to it. Is there any built in
support for this kind of thing?
Thanks,
Dave