由于IEnumerator使用情况下对象的当前状态,操作无效

时间:2015-07-08 01:19:08

标签: c# json xml ienumerator

我遇到一个问题,我有一个定义的IEnumerator变量,它保存了从XML文档中剥离的一些JSON。在JSON中,我将根据用户想要摆脱的内容去除碎片。但是,在IEnumerator的第二次使用时,我得到Operation not valid due to the current state of the object。我知道使用它/第二次调用它是一个问题,因为当我注释掉第一次使用时,它运行正常。但是,我最终会分三次打电话。

示例代码

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;
            }
        }
   }

我可以采用简单的路径并创建另一个IEnumerator变量,但我将在第二次迭代(和第三次)中需要维护第一次迭代的输出的场景。我正在阅读有关如何在maxhttpkeys的配置文件中添加密钥的一些内容,但我尝试了它并且它没有工作(如预期的那样)。我认为我的问题是因为我不止一次打电话给IEnumerator。我只是不确定如何解决它,因为我没有在SOF或谷歌上找到任何运气。

0 个答案:

没有答案