为什么我在listview中点击一条记录删除,记录没有了,可以还存在一个空行呢
<Datas>
  <Connection>
    <database>start</database>
    <basetype>Oracle</basetype>
    <username>scott</username>
    <password>tiger</password>
  </Connection>
  <Connection>
    <database>goodluck</database>
    <basetype>Oracle</basetype>
    <username>scott</username>
    <password>tiger</password>
  </Connection>
  <Connection>
    <database>hello</database>
    <basetype>Oracle</basetype>
    <username>scott</username>
    <password>tiger</password>
  </Connection>
  <Connection />
</Datas>代码如下:
                ListViewItem LVItems = listView1.FocusedItem;
                XElement xel = XElement.Load("C:\\DataConfig.xml");
                var item = from x in xel.Descendants("Connection")
                        where (string)x.Element("database") == LVItems.SubItems[0].Text
                        select x;
                foreach (var c in item)
                {
                    c.RemoveAll();
                }
                xel.Save("C:\\DataConfig.xml");
请问高手们如何解决这个问题?

解决方案 »

  1.   

    你的item应该就是<Connection>然后你调用item.RemoveAll,将<Connection>的内容情况.当然<Connection>还在了.试一试调用RemoveRemoveAll的解释:Removes nodes and attributes from this XElement.
    Remove的解释:Removes this node from its parent.
      

  2.   

    findcaiyzh:你的方法是对的,我把foreach那个循环变成了item.Remove(),问题就解决了,谢谢了