LINQ To Xml:按条件删除element

XDocument xDocument = new XDocument( new XElement("BookParticipants", new XElement("BookParticipant", new XAttribute("type", "Author"), new XElement("FirstName", "Joe"), new XElement("LastName", "Rattz")), new XElement("BookParticipant", new XAttribute("type", "Editor"), new XElement("FirstName", "Ewan"), new XElement("LastName", "Buckingham")))); xDocument.Descendants().Where(e => e.Name == "FirstName").Remove(); Console.WriteLine(xDocument);  

 

输出

 

<BookParticipants> <BookParticipant type="Author"> <LastName>Rattz</LastName> </BookParticipant> <BookParticipant type="Editor"> <LastName>Buckingham</LastName> </BookParticipant> </BookParticipants>  

你可能感兴趣的:(LINQ To Xml:按条件删除element)