C# tutorial - linq to read, insert, edit, and delete xml elements

Using linq to read, add, edit, and delete xml elements

In the previous post, you learned how to create an xml file from a collection of Book objects with Linq. Now, you will learn how to read, add, edit, and delete elements of the xml file that is stored in your computer.

Here is the XML file created in the previous lesson.

create xml file with linq

Read xml file

By using Linq query expression, you can easily read all elements of the xml file. The example code below gets all ements from the xml file, stores them in elements collection variable and outputs titles of books in the collection.

XDocument xmlDoc = XDocument.Load("d:/data.xml");
var elements= from ele in xmlDoc.Elements("Books").Elements("Book")
where ele != null
select ele;

foreach (var e in elements)

Add a new Elements to the xml file

The Linq expression to add a new element to the xml file is similary to reading from that file. However, you will use the Add method to add a new element to the file. In the example code below, a new element that has "AJAX" title attribute, and two sub-elements: author and year is added to the file.

XDocument xmlDoc = XDocument.Load("d:/data.xml");
.First().Add(new XElement("Book", new XAttribute("title", "AJAX"), new XElement("author", "Khemrin"), new XElement("year", "2000")));

Edit an element of the xml file

Simetimes, you might want change or udate a element of xml file. It is not hard to do this task by using Linq. The process is simple. You need to query the elements that meet your criteria. Then assign new values to their attributes or sub-elements. The code below will update the author sub-element of a Book element that has "Java" attribute title and its "Dara" author sub-element.

XDocument xmlDoc = XDocument.Load("d:/data.xml");
var items = from item in xmlDoc.Elements("Books").Elements("Book")
where item!=null && (item.Element("author").Value == "Dara" && item.Attribute("title").Value == "Java")
select item;

foreach (var item in items)

//assign new value to the sub-element author
item.Element("author").Value = "K Lavy";



Delete an element from the xml file

To delete an element from the xml file, you need to select the element based on your conditions. Then you will use the Remove method of the XElement class to delete that element. Here is an example code that removes the element that has "Java" title attribute.

XDocument xmlDoc = XDocument.Load("d:/data.xml");
var elementsToDelete = from ele in xmlDoc.Elements("Books").Elements("Book")
where ele != null && ele.Attribute("title").Value.Equals("Java")
select ele;

foreach (var e in elementsToDelete)




Naga comment


How to read, store and delete xml data using AbbCode Framework
DataTable dt = ac.ReadXmlData("~/App_Data/XML/persons.xml", "/Persons/Person[@name='Lisa Carter' or @name='Customer Name']/Gender", "p", "http://www.w3schools.com");
string[][] childNodes = new string[][] { new string[] { "userName", "", "ahmedf" } };
ac.InsertXmlData("~/App_Data/XML/users.xml", "users", "", "", childNodes);
ac.DeleteXmlDataByExpression("~/App_Data/XML/persons.xml", "Persons/Person[Age='27']");

Ha Vinh Hai comment

 Ha Vinh Hai

It is great !
Many thanks

Jumuro comment


Many thanks for sharing!

Shake comment


This is a very good C# linq tutorial. Thank you very much.


This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.