内容如题。。在C#里改如何写呢?
本来思路是先比对节点名称,如是的话就保存要删除节点的下标放在数组里。然后循环删除,但发现。。当我删除第一个后,所有节点下标数量都发生改变。。
就不能再按照之前纪录的节点下标删除了,下标数字都发生改变了。请高手指定给段代码!!!!
我这个xml有可能会多人操作的。。所以不能直接全部清空channel下所有的item,比对a节点InnerText如果符合再删除这个item下所有子节点。。包括item本身父节点
xml结构如下 <option>
<channel>
   <item>  
      <a> r</a>
      <b>
      <c>
</item>
   <item> 
       <a> f</a>
       <b>
       <c>
</item>
    

解决方案 »

  1.   

    XmlNodeList xnl=xmlDoc.SelectSingleNode("").ChildNodes; 
      foreach(XmlNode xn in xnl) 
      { 
        XmlElement xe=(XmlElement)xn; 
        if(xe.GetAttribute("name")=="a") 
        { 
        xe.RemoveAttribute("name");//删除属性 
        } 
        else if(xe.GetAttribute("name")=="b") 
        { 
        xe.RemoveAll();//删除该节点的全部内容 
        } 
      } 
      xmlDoc.Save("a.xml"); 
      

  2.   

    找到你符合你条件的item节点,删除就是了。比如:
    XmlDocument xmldoc = new XmlDocument();
    XmlNode node = xmldoc.SelectSingleNode("//channel/item[a='r']");
    if (node != null)
    {
        xmldoc.RemoveChild(node);
    }
    xmldoc.Save("aa");
      

  3.   


    private void DeleteNode(string value)
    {
      XmlDocument xmldoc = new XmlDocument();
      xmldoc.Load("路径");
      XmlNode node = xmldoc.SelectSingleNode("//channel/item[a='"+value+"']");
      if (node != null)
      {
        xmldoc.RemoveChild(node);
       }
       xmldoc.Save("路径");
    }
      

  4.   

     private void DeleteNode(string value)
        {
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.Load(Server.MapPath("myFlv.xml"));
            XmlNode nodes = xmldoc.SelectSingleNode("ucfar/channel");
            XmlNodeList xnl = nodes.ChildNodes;
            foreach(XmlNode node in xnl)
            {
                if (node.Name == "item")
                {
                    XmlNodeList itemlist = node.ChildNodes;                foreach (XmlNode ItNode in itemlist)
                    {
                       // xnl.RemoveChild(node);
                        if (ItNode.Name == "AWB"&&ItNode.InnerText==value)
                        {
                          
                            nodes.RemoveChild(node); 
    //应该是channel下符合条件的item整个移除才对,但却每次只删除了一个itme 后就不删除后面符合条件的了。。这是这么回事呢?
                            xmldoc.Save(Server.MapPath("myFlv.xml"));
                        }
                                       }            }
              
            }
           
        }
      

  5.   


    private void DeleteNode(string value) 
        { 
            XmlDocument xmldoc = new XmlDocument(); 
            xmldoc.Load(Server.MapPath("myFlv.xml")); 
            XmlNode nodes = xmldoc.SelectSingleNode("ucfar/channel"); 
            XmlNodeList xnl = nodes.ChildNodes; 
            foreach(XmlNode node in xnl) 
            { 
                if (node.Name == "item") 
                { 
                    XmlNodeList itemlist = node.ChildNodes;                 foreach (XmlNode ItNode in itemlist) 
                    { 
                        if (ItNode.Name == "AWB"&&ItNode.InnerText==value) 
                        { 
                            nodes.RemoveChild(node); 
                        } 
                    }             } 
              
            } 
            xmldoc.Save(Server.MapPath("myFlv.xml")); 
        }
    试试