Read Write and Update XML using LINQ Queries

I am going to take a Simple XML data and will show how to read write and Update it using LINQ.

Sample XML File:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LumiaMobiles>  
  3.   <LumiaMobile>  
  4.     <ModelId>lumia550</ModelId>  
  5.     <Height>136.1 mm</Height>  
  6.     <Width>67.8 mm</Width>  
  7.     <Thickness>9.9 mm</Thickness>  
  8.     <Weight>141.9 g</Weight>  
  9.   </LumiaMobile>  
  10.   <LumiaMobile>  
  11.     <ModelId>lumia950</ModelId>  
  12.     <Height>145 mm</Height>  
  13.     <Width>73.2 mm</Width>  
  14.     <Thickness>8.2 mm</Thickness>  
  15.     <Weight>150 g</Weight>  
  16.   </LumiaMobile>  
  17. </LumiaMobiles>  
Corresponding class for XML Data:
  1. public class LumiaMobile  
  2. {  
  3.    public string ModelId { getset; }  
  4.    public string Height { getset; }  
  5.    public string Width { getset; }  
  6.    public string Thickness { getset; }  
  7.    public string Weight { getset; }  
  8. }  
Code to Write XML File
  1. private void WriteLumiaMobilesDetails(List<LumiaMobile> lstLumiaMobiles)  
  2. {  
  3.     using (XmlWriter writer = XmlWriter.Create("LumiaMobiles2.xml"))  
  4.     {  
  5.         writer.WriteStartDocument();  
  6.         writer.WriteStartElement("LumiaMobiles");  
  7.         foreach (var mobile in lstLumiaMobiles)  
  8.         {  
  9.             writer.WriteStartElement("LumiaMobile");  
  10.             writer.WriteElementString("ModelId", mobile.ModelId);  
  11.             writer.WriteElementString("Height", mobile.Height);  
  12.             writer.WriteElementString("Width", mobile.Width);  
  13.             writer.WriteElementString("Thickness", mobile.Thickness);  
  14.             writer.WriteElementString("Weight", mobile.Weight);  
  15.             writer.WriteEndElement();  
  16.         }  
  17.   
  18.         writer.WriteEndElement();  
  19.         writer.WriteEndDocument();  
  20.     }  
  21. }  
Calling Write XML Data Method:
  1. static void Main(string[] args)  
  2. {  
  3.     Program p = new Program();              
  4.     List<LumiaMobile> lstLumiaMobiles = new List<LumiaMobile>();  
  5.     lstLumiaMobiles.Add(new LumiaMobile  
  6.     {  
  7.         ModelId = "lumia550",  
  8.         Height = "136.1 mm",  
  9.         Width = "67.8 mm",  
  10.         Thickness = "9.9 mm",  
  11.         Weight = "141.9 g"  
  12.     });  
  13.   
  14.     lstLumiaMobiles.Add(new LumiaMobile  
  15.     {  
  16.         ModelId = "lumia950",  
  17.         Height = "145 mm",  
  18.         Width = "73.2 mm",  
  19.         Thickness = "8.2 mm",  
  20.         Weight = "150 g"  
  21.     });  
  22.     p.WriteLumiaMobilesDetails(lstLumiaMobiles);              
  23. }  
Code to Read XML Data:
  1. public List<LumiaMobile> ReadLumiaMobilesDetails()  
  2. {  
  3.     List<LumiaMobile> lstLumiaMobiles = new List<LumiaMobile>();  
  4.     try  
  5.     {  
  6.         using (XmlReader xr = XmlReader.Create(@"LumiaMobiles.xml"))  
  7.         {  
  8.             XDocument xdoc = XDocument.Load(xr);  
  9.             var LumiaMobiles = xdoc.Descendants("LumiaMobiles").Descendants("LumiaMobile");  
  10.             foreach (var mobile in LumiaMobiles)  
  11.             {  
  12.                 lstLumiaMobiles.Add(new LumiaMobile  
  13.                 {  
  14.                     ModelId = Convert.ToString(mobile.Descendants("ModelId").FirstOrDefault().Value),  
  15.                     Height = Convert.ToString(mobile.Descendants("Height").FirstOrDefault().Value),  
  16.                     Width = Convert.ToString(mobile.Descendants("Width").FirstOrDefault().Value),  
  17.                     Thickness = Convert.ToString(mobile.Descendants("Thickness").FirstOrDefault().Value),  
  18.                     Weight = Convert.ToString(mobile.Descendants("Weight").FirstOrDefault().Value)  
  19.                 });  
  20.             }  
  21.         }  
  22.     }  
  23.     catch (Exception)  
  24.     {  
  25.   
  26.     }  
  27.     return lstLumiaMobiles;  
  28. }  
Calling Read XML Data Method:
  1. static void Main(string[] args)  
  2. {  
  3.    Program p = new Program();  
  4.    p.ReadLumiaMobilesDetails();   
  5. }  
Code to Update XML Data:
  1. private void UpdateXmlElementforMobile(string ModelId, string element, string elementValue)  
  2. {  
  3.   
  4.     XDocument xdoc = XDocument.Load("LumiaMobiles.xml");  
  5.     var LumiaMobiles = xdoc.Descendants("LumiaMobile");  
  6.     foreach (var mobile in LumiaMobiles)  
  7.     {  
  8.         if (mobile.Descendants("ModelId").Any())  
  9.         {  
  10.             if (Convert.ToString(mobile.Descendants("ModelId").FirstOrDefault().Value) == ModelId)  
  11.             {  
  12.                 mobile.Descendants(element).FirstOrDefault().Value = elementValue;  
  13.                 xdoc.Save("LumiaMobiles.xml");  
  14.             }  
  15.         }  
  16.     }  
  17. }  
Calling Update XML Data Method:
  1. static void Main(string[] args)  
  2. {  
  3.    Program p = new Program();  
  4.    p.UpdateXmlElementforMobile("lumia950""Height""145.0 mm");  
  5. }  
Complete Code is:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Xml;  
  5. using System.Xml.Linq;  
  6.   
  7. namespace XMLtoLINQ  
  8. {  
  9.     class Program  
  10.     {  
  11.         static void Main(string[] args)  
  12.         {  
  13.             Program p = new Program();  
  14.             p.ReadLumiaMobilesDetails();  
  15.             List<LumiaMobile> lstLumiaMobiles = new List<LumiaMobile>();  
  16.             lstLumiaMobiles.Add(new LumiaMobile  
  17.             {  
  18.                 ModelId = "lumia550",  
  19.                 Height = "136.1 mm",  
  20.                 Width = "67.8 mm",  
  21.                 Thickness = "9.9 mm",  
  22.                 Weight = "141.9 g"  
  23.             });  
  24.   
  25.             lstLumiaMobiles.Add(new LumiaMobile  
  26.             {  
  27.                 ModelId = "lumia950",  
  28.                 Height = "145 mm",  
  29.                 Width = "73.2 mm",  
  30.                 Thickness = "8.2 mm",  
  31.                 Weight = "150 g"  
  32.             });  
  33.   
  34.             p.WriteLumiaMobilesDetails(lstLumiaMobiles);  
  35.   
  36.             p.UpdateXmlElementforMobile("lumia950""Height""145.0 mm");  
  37.         }  
  38.   
  39.         private void UpdateXmlElementforMobile(string ModelId, string element, string elementValue)  
  40.         {  
  41.   
  42.             XDocument xdoc = XDocument.Load("LumiaMobiles.xml");  
  43.             var LumiaMobiles = xdoc.Descendants("LumiaMobile");  
  44.             foreach (var mobile in LumiaMobiles)  
  45.             {  
  46.                 if (mobile.Descendants("ModelId").Any())  
  47.                 {  
  48.                     if (Convert.ToString(mobile.Descendants("ModelId").FirstOrDefault().Value) == ModelId)  
  49.                     {  
  50.                         mobile.Descendants(element).FirstOrDefault().Value = elementValue;  
  51.                         xdoc.Save("LumiaMobiles.xml");  
  52.                     }  
  53.                 }  
  54.             }  
  55.         }  
  56.   
  57.         private void WriteLumiaMobilesDetails(List<LumiaMobile> lstLumiaMobiles)  
  58.         {  
  59.             using (XmlWriter writer = XmlWriter.Create("LumiaMobiles2.xml"))  
  60.             {  
  61.                 writer.WriteStartDocument();  
  62.                 writer.WriteStartElement("LumiaMobiles");  
  63.                 foreach (var mobile in lstLumiaMobiles)  
  64.                 {  
  65.                     writer.WriteStartElement("LumiaMobile");  
  66.                     writer.WriteElementString("ModelId", mobile.ModelId);  
  67.                     writer.WriteElementString("Height", mobile.Height);  
  68.                     writer.WriteElementString("Width", mobile.Width);  
  69.                     writer.WriteElementString("Thickness", mobile.Thickness);  
  70.                     writer.WriteElementString("Weight", mobile.Weight);  
  71.                     writer.WriteEndElement();  
  72.                 }  
  73.   
  74.                 writer.WriteEndElement();  
  75.                 writer.WriteEndDocument();  
  76.             }  
  77.         }  
  78.   
  79.         public List<LumiaMobile> ReadLumiaMobilesDetails()  
  80.         {  
  81.             List<LumiaMobile> lstLumiaMobiles = new List<LumiaMobile>();  
  82.             try  
  83.             {  
  84.                 using (XmlReader xr = XmlReader.Create(@"LumiaMobiles.xml"))  
  85.                 {  
  86.                     XDocument xdoc = XDocument.Load(xr);  
  87.                     var LumiaMobiles = xdoc.Descendants("LumiaMobiles").Descendants("LumiaMobile");  
  88.                     foreach (var mobile in LumiaMobiles)  
  89.                     {  
  90.                         lstLumiaMobiles.Add(new LumiaMobile  
  91.                         {  
  92.                             ModelId = Convert.ToString(mobile.Descendants("ModelId").FirstOrDefault().Value),  
  93.                             Height = Convert.ToString(mobile.Descendants("Height").FirstOrDefault().Value),  
  94.                             Width = Convert.ToString(mobile.Descendants("Width").FirstOrDefault().Value),  
  95.                             Thickness = Convert.ToString(mobile.Descendants("Thickness").FirstOrDefault().Value),  
  96.                             Weight = Convert.ToString(mobile.Descendants("Weight").FirstOrDefault().Value)  
  97.                         });  
  98.                     }  
  99.                 }  
  100.             }  
  101.             catch (Exception)  
  102.             {  
  103.   
  104.             }  
  105.             return lstLumiaMobiles;  
  106.         }  
  107.     }  
  108.   
  109.     public class LumiaMobile  
  110.     {  
  111.         public string ModelId { getset; }  
  112.         public string Height { getset; }  
  113.         public string Width { getset; }  
  114.         public string Thickness { getset; }  
  115.         public string Weight { getset; }  
  116.     }  
  117. }