string id_name = e.CommandArgument.ToString();
        //删除节点为id_name的节点
        int i = id_name.Length;
   
        string xmlPath = Session["url"].ToString();
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(xmlPath);         XmlNodeList xnl = xmlDoc.SelectSingleNode("Products").ChildNodes;        foreach (XmlNode xn in xnl)
        {
            XmlElement xe = (XmlElement) xn;   //此处引起错误提示
            if (xe.GetAttribute("time_id") == id_name)
            {
                xe.RemoveAttribute("time_id");//删除genre属性 
            }
            //else if (xe.GetAttribute("genre") == "update陶维佳")
            //{
            //    xe.RemoveAll();//删除该节点的全部内容 
            //}
        }
        xmlDoc.Save(xmlPath); --------------]
我想删除
<?xml version="1.0" encoding="utf-8"?>
<Products>
<Product id="肉菜" Name="猴子 " Price="1.5" time_id="2007-11-29 04-50-03" />
<Product id="汤" Name="蟋蟀 " Price="23" time_id="2007-11-29 04-50-04" />
<Product id="肉菜" Name="羊  " Price="8.3" time_id="2007-11-29 04-50-09" />
<Product id="肉菜" Name="猴子 " Price="1.5" time_id="2007-11-29 04-50-10" />
<Product id="肉菜" Name="猪  " Price="2" time_id="2007-11-29 04-50-11" />
</Products>我想删除的是 time_id="2007-11-29 04-50-03" 的这个节点无法将类型为“System.Xml.XmlText”的对象强制转换为类型“System.Xml.XmlElement”
总是有上面的错误提示

解决方案 »

  1.   


               foreach (XmlNode xn in xnl)
                {
                    if (xn.Attributes["time_id"].Value == id_name)
                    {
                        xn.ParentNode.RemoveChild(xn);
                    }
                } 
      

  2.   

    按一楼那样你可以取到。
    但你做的过程中应该还会有其他错误发生的。foreach 是只读的。以前删的时候只删除一次就会异常跳出了,最后的结果就是只删除了一条,如果后面也有如何条件的,就删不掉。