0
<Albums>
  <Album ID="ankit">
    <AlbumName>Jaipur Tour</AlbumName>
    <AlbumName>Shimla Tour</AlbumName>
    <AlbumName>Andaman Tour</AlbumName>
    <AlbumName>France Tour</AlbumName>
  </Album>
  <Album ID="amit">
    <AlbumName>Goa Tour</AlbumName>
    <AlbumName>China Tour</AlbumName>
    <AlbumName>Panama Tour</AlbumName> // delete this node
    <AlbumName>Europe Tour</AlbumName>
  </Album>
</Albums>

从给定用户 ID 并在该用户 ID 下给定专辑名称的 album.xml 中删除专辑

var qryAlbum = from element in loadAlbum.Descendants("Album")
where (string)element.Attribute("ID") == UIDLabel.Text
select element;
qryAlbum.Descendants("Album").Elements("AlbumName").Where(x => x.Value == albumSelected).Remove();

 loadAlbum.Save(@"D:\PhotoFrame\Album.xml");
4

1 回答 1

1

您需要选择要删除的元素。

 var element = loadAlbum.Descendants("AlbumName")
                        .Where(x => x.Value == "Panama Tour" 
                         && x.Parent.Attribute("ID").Value == "amit" )
                        .FirstOrDefault();
 if (element != null)
  {
   element.Remove();
   loadAlbum.Save(@"file.xml");
  }

编辑:更换text

if (element != null)
      {
       element.Value = "New Value";
       loadAlbum.Save(@"file.xml");
      }
于 2013-06-24T05:37:22.727 回答