我的XML文件格式如下 
<AllRecords Rec="查看全部"> 
   <Item Category="按客户查看"> 
     <CustomerName>A </CustomerName> 
     <CustomerName>B </CustomerName> 
     <CustomerName>C </CustomerName> 
     <CustomerName>D </CustomerName> 
   </Item> 
   <Item Category="按时间查看"> 
     <CustomerName>1 </CustomerName> 
   </Item> 
</AllRecords> 
我现在想删除 <CustomerName>D </CustomerName>这行,该如何操作?谢谢!!请具体点,马上送分,谢谢!

解决方案 »

  1.   


    XmlNode parent = xmlDoc.SelectSingleNode("Item[@Category='按客户查看']");
    XmlNode subNode = parent.SelectSingleNode("CustomerName[text() = 'D']");
    parent.RemoveChild(subNode);
      

  2.   

    又被人抢了..哎
    1楼的xmlpath可以实现
      

  3.   

    XmlDocument document = new XmlDocument();
    document.Load(xmlPath);
    string strFind = String.Format("//AllRecords/Item[@Category=\"{0}\"]", "按客户查看");
    System.Xml.XmlNodeList nodeList = document.SelectNodes(strFind);
    foreach (System.Xml.XmlNode node in nodeList.ChildNodes)
     {
                   
         System.Xml.XmlElement xep = (System.Xml.XmlElement)node;
         
                    if (xep.InnerText== "D")
                    {
                        nodeList.RemoveChild(node);
                        break;
                    }
     }
    document.Save(xmlPath);
    没有测试,你自己测试一下
      

  4.   

    ismezy2002里面这行XmlNode parent = xmlDoc.SelectSingleNode("Item[@Category='按客户查看']");换成string strFind = String.Format("//AllRecords/Item[@Category=\"{0}\"]", "按客户查看"); 
    XmlNode parent = xmlDoc.SelectSingleNode(strFind);就可以了,完整程序为:...
    private XmlDocument xmlDoc;
    private void LoadXml()
      {
        xmlDoc = new XmlDocument();
        xmlDoc.Load(Server.MapPath("tree.xml"));
      }
    LoadXml();
    string strFind = String.Format("//AllRecords/Item[@Category=\"{0}\"]", "按客户查看");
    string strCustomerName = ((Label)(GridView1.Rows[e.RowIndex].Cells[1].Controls[1])).Text.ToString();//此处选出gridview所选行文本
    XmlNode parent = xmlDoc.SelectSingleNode(strFind);
    XmlNode subNode = parent.SelectSingleNode("CustomerName[text() ='"+strCustomerName+"']");
    parent.RemoveChild(subNode);
    xmlDoc.Save(Server.MapPath("tree.xml"));
    ...
      

  5.   

    另外xieyongbao朋友的程序不能通过 System.Xml.XmlNode node in nodeList.ChildNodes nodelist里面没有那些属性,不过他的应该思路可以实现