当前位置:编程学习 > C#/ASP.NET >>

(元素列表已更改。枚举操作未能继续。XML)在线等待.....明天上交.谢了各位!~

小弟想根据XML某元素的属性值更改值或删除指定的节点.
然后报错....
明天要上交了.
求求大家帮个忙!~谢谢了
代码如下

public void rnode(string teln,string num)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load("phont.xml");
            XmlNode info = doc.DocumentElement;
            XmlNodeList nldes = doc.GetElementsByTagName("user");
            foreach (XmlNode nlde in nldes)
            {
                XmlElement xe = (XmlElement)nlde;
                if (xe.GetAttribute("telno") == teln)
                {
                    xe.SetAttribute("yue", num);
                }
            }
        }


报错内容如下************** 异常文本 **************
System.InvalidOperationException: 元素列表已更改。枚举操作未能继续。
   在 System.Xml.XmlElementListEnumerator.MoveNext()
   在 telInfo.telact.rnode(String teln, String num) 位置 D:\C#项目\手机管理\telInfo\telInfo\telact.cs:行号 138
   在 telInfo.Form4.button1_Click(Object sender, EventArgs e) 位置 D:\C#项目\手机管理\telInfo\telInfo\Form4.cs:行号 46
   在 System.Windows.Forms.Control.OnClick(EventArgs e)
   在 System.Windows.Forms.Button.OnClick(EventArgs e)
   在 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   在 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   在 System.Windows.Forms.Control.WndProc(Message& m)
   在 System.Windows.Forms.ButtonBase.WndProc(Message& m)
   在 System.Windows.Forms.Button.WndProc(Message& m)
   在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   在 System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


************** 已加载的程序集 **************
mscorlib
    程序集版本: 2.0.0.0
    Win32 版本: 2.0.50727.42 (RTM.050727-4200)
    基本代码: file:///C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/mscorlib.dll
----------------------------------------
telInfo
    程序集版本: 1.0.0.0
    Win32 版本: 1.0.0.0
    基本代码: file:///D:/C%23项目/手机管理/telInfo/telInfo/bin/Debug/telInfo.exe
----------------------------------------
System.Windows.Forms
    程序集版本: 2.0.0.0
    Win32 版本: 2.0.50727.42 (RTM.050727-4200)
    基本代码: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Windows.Forms/2.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System
    程序集版本: 2.0.0.0
    Win32 版本: 2.0.50727.42 (RTM.050727-4200)
    基本代码: file:///C:/WINDOWS/assembly/GAC_MSIL/System/2.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Drawing
    程序集版本: 2.0.0.0
    Win32 版本: 2.0.50727.42 (RTM.050727-4200)
    基本代码: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Drawing/2.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
System.Configuration
    程序集版本: 2.0.0.0
    Win32 版本: 2.0.50727.42 (RTM.050727-4200)
    基本代码: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Configuration/2.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
----------------------------------------
System.Xml
    程序集版本: 2.0.0.0
    Win32 版本: 2.0.50727.42 (RTM.050727-4200)
    基本代码: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Xml/2.0.0.0__b77a5c561934e089/System.Xml.dll
----------------------------------------
System.XML.resources
    程序集版本: 2.0.0.0
    Win32 版本: 2.0.50727.42 (RTM.050727-4200)
    基本代码: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Xml.resources/2.0.0.0_zh-CHS_b77a5c561934e089/System.Xml.resources.dll
----------------------------------------
System.Windows.Forms.resources
    程序集版本: 2.0.0.0
    Win32 版本: 2.0.50727.42 (RTM.050727-4200)
    基本代码: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Windows.Forms.resources/2.0.0.0_zh-CHS_b77a5c561934e089/System.Windows.Forms.resources.dll
----------------------------------------
mscorlib.resources
    程序集版本: 2.0.0.0
    Win32 版本: 2.0.50727.42 (RTM.050727-4200)
    基本代码: file:///C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/mscorlib.dll
----------------------------------------

************** JIT 调试 **************
要启用实时(JIT)调试,
该应用程序或计算机的 .config 文件(machine.config)的 system.windows.forms 节中必须设置
jitDebugging 值。
编译应用程序时还必须启用
调试。

例如: 

<configuration>
    <system.windows.forms jitDebugging="true" />
</configuration>

启用 JIT 调试后,任何无法处理的异常
都将被发送到在此计算机上注册的 JIT 调试器,
而不是由此对话框处理。 --------------------编程问答-------------------- 在GOOGLE和百度都没找到相关或相似的内容......

貌似是很少见的东西吧

难怪没人来.. --------------------编程问答--------------------  // 注意在 forech中不能进行对元素的修该,除非用for循环
                    //System.InvalidOperationException:修改集合;枚举操作可能无法执行(2)


所以这里使用到了For循环完成了相应的操作的
2011年3月13日16:06:05


//GetElementsByTagName("Items"); 表示通过Items的Name获取全部的元素
                    XmlNodeList xmlnodelist = xmlDcombox.GetElementsByTagName("Items");




                    for (int i = 0; i < xmlnodelist.Count; i++)
                    {
                        if (xmlnodelist[i].InnerText == selectItems)
                        {
                            
                                 comboBox1消费种类.Items.Remove(selectItems);
                            xmlnodelist[i].OwnerDocument.DocumentElement.RemoveChild(xmlnodelist[i]);


                        }
                    }
 
                        
          
                    //foreach (XmlNode xn in xmlnodelist)
                    //{

                    //    //获取所有子元素的串联质
                    //    if (xn.InnerText == selectItems)
                    //    {
                    //        xnn = xn.Clone() ;


                    //        //   xn.OwnerDocument.DocumentElement.RemoveChild(xn);


                    //    }
                    //} --------------------编程问答-------------------- foreach里把要循环的东西改了,再foreach就会出错
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,