0

I am having an issue where I have a defined IEnumerator variable that holds some JSON stripped from an XML Document. Within the JSON, I will be stripping bits and pieces out depending on what the user wants to get rid of. However, on the second usage of the IEnumerator, I get Operation not valid due to the current state of the object. I know it's an issue with using it/calling it a second time because when I comment out the first usage, it works fine. However, I will eventually be calling this 3 separate times.

Sample Code

private XmlDocument deleteXml(XmlDocument xmlDoc)
{
    IEnumerator theJson = xmlDoc.SelectNodes("Path/Path2").GetEnumerator();

    KillNode(theJson, "one");
    KillNode(theJson, "two"); //operation not valid due to the current state of the object
}

private void KillNode(IEnumerator theNodes, string nodeType)
    {

        while (theNodes.MoveNext())
        {
            XmlNode theNode = theNodes.Current as XmlNode;

            switch (nodeType)
            {
                case "one":
                    //do something
                break;
                case "two":
                    //do something
                break;
            }
        }
   }

I could take the easy route and just create another IEnumerator variable but I will have the scenario where the output of the first iteration needs to be maintained in the second iteration (and third). I'm reading some things about how I need to add a key to the config file for maxhttpkeys but I tried that and it didn't work (as expected). I think my issue is because I'm calling the IEnumerator more than once. I'm just not sure how to solve it as I am not having any luck finding anything on SOF or google.

mwilson
  • 12,295
  • 7
  • 55
  • 95
  • If you feel [Marc Gravell's answer](http://stackoverflow.com/a/5968973/18771) does not apply to your situation, say so and I'll re-open the question. – Tomalak Jul 08 '15 at 01:29
  • Yea, that does not apply. That's just a thread saying to not use Reset() and keep doing GetEnumerator() which i think will void my requirement of keeping the integrity of the content. – mwilson Jul 08 '15 at 01:29
  • 1
    I know you have not been trying to call `Reset`. But the essence is "Enumerators are not generally repeatable, just get a new Enumerator or buffer them in a list", I think that part applies, doesn't it? – Tomalak Jul 08 '15 at 01:31
  • I agree with you there. However, if I lose the integrity of the content, it's not the right thread and isn't relative to what i'm trying to do. Testing now, though. – mwilson Jul 08 '15 at 01:34
  • Good to hear! Thank Marc by voting him up. ;) – Tomalak Jul 08 '15 at 02:00

0 Answers0