I've changed the XML file so that each student's data is contained within an individual <student> ... </student> element:
<students>
<student>
<name>Nick Young</name>
<address>8th Street/</address>
<phone>0123456789</phone>
</student>
<student>
<name>George Old</name>
<address>9th Street/</address>
<phone>1234567890</phone>
</student>
</students>
Suppose now that you want to delete Nick Young from the file. The following LINQ to XML code will do it:
using System;
using System.Linq;
using System.Xml.Linq;
class Test
{
static void Main()
{
XElement students = XElement.Load("seny.xml");
string findName = "Nick Young";
XElement foundElement = (from student in students.Elements("student") where student.Element("name").Value == findName select student).FirstOrDefault();
if (foundElement != null)
{
foundElement.Remove(); // removes it from its parent
Console.WriteLine("{0} found and deleted", findName);
students.Save("seny2.xml");
}
else
{
Console.WriteLine("{0} not present in XML file", findName);
}
Console.ReadKey();
}
}
After running this code, the file should look like this:
<students>
<student>
<name>George Old</name>
<address>9th Street/</address>
<phone>1234567890</phone>
</student>
</students>