XML删除指定节点

  今天使用C#操作XML读取配置文件,删除指定节点。本来很简单,但是不注意就会出错哦,拿出来分享下经验

     public void Remove(VideoSource videoSource)
        {

        String innerText = videoSource.TypeID + videoSource.Name + videoSource.ThumbPath + videoSource.VideoPath;

           XmlDocument xml=new XmlDocument();            

    xml.Load(System.Windows.Forms.Application.StartupPath + "/XML/VideoSourceXML.xml");

    //获取第一个VideoSources下所有的子节点

           XmlNodeList xmlNodeList = xml.SelectSingleNode("VideoSources").ChildNodes;

           foreach (XmlNode item in xmlNodeList)

           {                

       if (item.InnerText == innerText)                

      {                    

        //PS:如果你要是xml.RemoveChild(item);那么肯定会报移除得节点不是xml的子节点。

        xml.SelectSingleNode("VideoSources").RemoveChild(item);

          //item.RemoveAll();可以删除item下的所有子节点

                    break;

                }

           }

           xml.Save(System.Windows.Forms.Application.StartupPath + "/XML/VideoSourceXML.xml");

  }

posted on 2014-07-21 16:37  5ai11  阅读(1120)  评论(0编辑  收藏  举报

导航