I am going to take a Simple XML data and will show how to read write and Update it using LINQ.
Sample XML File:
- <?xml version="1.0" encoding="utf-8"?>
- <LumiaMobiles>
- <LumiaMobile>
- <ModelId>lumia550</ModelId>
- <Height>136.1 mm</Height>
- <Width>67.8 mm</Width>
- <Thickness>9.9 mm</Thickness>
- <Weight>141.9 g</Weight>
- </LumiaMobile>
- <LumiaMobile>
- <ModelId>lumia950</ModelId>
- <Height>145 mm</Height>
- <Width>73.2 mm</Width>
- <Thickness>8.2 mm</Thickness>
- <Weight>150 g</Weight>
- </LumiaMobile>
- </LumiaMobiles>
Corresponding class for XML Data:
- public class LumiaMobile
- {
- public string ModelId { get; set; }
- public string Height { get; set; }
- public string Width { get; set; }
- public string Thickness { get; set; }
- public string Weight { get; set; }
- }
Code to Write XML File
- private void WriteLumiaMobilesDetails(List<LumiaMobile> lstLumiaMobiles)
- {
- using (XmlWriter writer = XmlWriter.Create("LumiaMobiles2.xml"))
- {
- writer.WriteStartDocument();
- writer.WriteStartElement("LumiaMobiles");
- foreach (var mobile in lstLumiaMobiles)
- {
- writer.WriteStartElement("LumiaMobile");
- writer.WriteElementString("ModelId", mobile.ModelId);
- writer.WriteElementString("Height", mobile.Height);
- writer.WriteElementString("Width", mobile.Width);
- writer.WriteElementString("Thickness", mobile.Thickness);
- writer.WriteElementString("Weight", mobile.Weight);
- writer.WriteEndElement();
- }
-
- writer.WriteEndElement();
- writer.WriteEndDocument();
- }
- }
Calling Write XML Data Method:
- static void Main(string[] args)
- {
- Program p = new Program();
- List<LumiaMobile> lstLumiaMobiles = new List<LumiaMobile>();
- lstLumiaMobiles.Add(new LumiaMobile
- {
- ModelId = "lumia550",
- Height = "136.1 mm",
- Width = "67.8 mm",
- Thickness = "9.9 mm",
- Weight = "141.9 g"
- });
-
- lstLumiaMobiles.Add(new LumiaMobile
- {
- ModelId = "lumia950",
- Height = "145 mm",
- Width = "73.2 mm",
- Thickness = "8.2 mm",
- Weight = "150 g"
- });
- p.WriteLumiaMobilesDetails(lstLumiaMobiles);
- }
Code to Read XML Data:
- public List<LumiaMobile> ReadLumiaMobilesDetails()
- {
- List<LumiaMobile> lstLumiaMobiles = new List<LumiaMobile>();
- try
- {
- using (XmlReader xr = XmlReader.Create(@"LumiaMobiles.xml"))
- {
- XDocument xdoc = XDocument.Load(xr);
- var LumiaMobiles = xdoc.Descendants("LumiaMobiles").Descendants("LumiaMobile");
- foreach (var mobile in LumiaMobiles)
- {
- lstLumiaMobiles.Add(new LumiaMobile
- {
- ModelId = Convert.ToString(mobile.Descendants("ModelId").FirstOrDefault().Value),
- Height = Convert.ToString(mobile.Descendants("Height").FirstOrDefault().Value),
- Width = Convert.ToString(mobile.Descendants("Width").FirstOrDefault().Value),
- Thickness = Convert.ToString(mobile.Descendants("Thickness").FirstOrDefault().Value),
- Weight = Convert.ToString(mobile.Descendants("Weight").FirstOrDefault().Value)
- });
- }
- }
- }
- catch (Exception)
- {
-
- }
- return lstLumiaMobiles;
- }
Calling Read XML Data Method:
- static void Main(string[] args)
- {
- Program p = new Program();
- p.ReadLumiaMobilesDetails();
- }
Code to Update XML Data:
- private void UpdateXmlElementforMobile(string ModelId, string element, string elementValue)
- {
-
- XDocument xdoc = XDocument.Load("LumiaMobiles.xml");
- var LumiaMobiles = xdoc.Descendants("LumiaMobile");
- foreach (var mobile in LumiaMobiles)
- {
- if (mobile.Descendants("ModelId").Any())
- {
- if (Convert.ToString(mobile.Descendants("ModelId").FirstOrDefault().Value) == ModelId)
- {
- mobile.Descendants(element).FirstOrDefault().Value = elementValue;
- xdoc.Save("LumiaMobiles.xml");
- }
- }
- }
- }
Calling Update XML Data Method:
- static void Main(string[] args)
- {
- Program p = new Program();
- p.UpdateXmlElementforMobile("lumia950", "Height", "145.0 mm");
- }
Complete Code is:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Xml;
- using System.Xml.Linq;
-
- namespace XMLtoLINQ
- {
- class Program
- {
- static void Main(string[] args)
- {
- Program p = new Program();
- p.ReadLumiaMobilesDetails();
- List<LumiaMobile> lstLumiaMobiles = new List<LumiaMobile>();
- lstLumiaMobiles.Add(new LumiaMobile
- {
- ModelId = "lumia550",
- Height = "136.1 mm",
- Width = "67.8 mm",
- Thickness = "9.9 mm",
- Weight = "141.9 g"
- });
-
- lstLumiaMobiles.Add(new LumiaMobile
- {
- ModelId = "lumia950",
- Height = "145 mm",
- Width = "73.2 mm",
- Thickness = "8.2 mm",
- Weight = "150 g"
- });
-
- p.WriteLumiaMobilesDetails(lstLumiaMobiles);
-
- p.UpdateXmlElementforMobile("lumia950", "Height", "145.0 mm");
- }
-
- private void UpdateXmlElementforMobile(string ModelId, string element, string elementValue)
- {
-
- XDocument xdoc = XDocument.Load("LumiaMobiles.xml");
- var LumiaMobiles = xdoc.Descendants("LumiaMobile");
- foreach (var mobile in LumiaMobiles)
- {
- if (mobile.Descendants("ModelId").Any())
- {
- if (Convert.ToString(mobile.Descendants("ModelId").FirstOrDefault().Value) == ModelId)
- {
- mobile.Descendants(element).FirstOrDefault().Value = elementValue;
- xdoc.Save("LumiaMobiles.xml");
- }
- }
- }
- }
-
- private void WriteLumiaMobilesDetails(List<LumiaMobile> lstLumiaMobiles)
- {
- using (XmlWriter writer = XmlWriter.Create("LumiaMobiles2.xml"))
- {
- writer.WriteStartDocument();
- writer.WriteStartElement("LumiaMobiles");
- foreach (var mobile in lstLumiaMobiles)
- {
- writer.WriteStartElement("LumiaMobile");
- writer.WriteElementString("ModelId", mobile.ModelId);
- writer.WriteElementString("Height", mobile.Height);
- writer.WriteElementString("Width", mobile.Width);
- writer.WriteElementString("Thickness", mobile.Thickness);
- writer.WriteElementString("Weight", mobile.Weight);
- writer.WriteEndElement();
- }
-
- writer.WriteEndElement();
- writer.WriteEndDocument();
- }
- }
-
- public List<LumiaMobile> ReadLumiaMobilesDetails()
- {
- List<LumiaMobile> lstLumiaMobiles = new List<LumiaMobile>();
- try
- {
- using (XmlReader xr = XmlReader.Create(@"LumiaMobiles.xml"))
- {
- XDocument xdoc = XDocument.Load(xr);
- var LumiaMobiles = xdoc.Descendants("LumiaMobiles").Descendants("LumiaMobile");
- foreach (var mobile in LumiaMobiles)
- {
- lstLumiaMobiles.Add(new LumiaMobile
- {
- ModelId = Convert.ToString(mobile.Descendants("ModelId").FirstOrDefault().Value),
- Height = Convert.ToString(mobile.Descendants("Height").FirstOrDefault().Value),
- Width = Convert.ToString(mobile.Descendants("Width").FirstOrDefault().Value),
- Thickness = Convert.ToString(mobile.Descendants("Thickness").FirstOrDefault().Value),
- Weight = Convert.ToString(mobile.Descendants("Weight").FirstOrDefault().Value)
- });
- }
- }
- }
- catch (Exception)
- {
-
- }
- return lstLumiaMobiles;
- }
- }
-
- public class LumiaMobile
- {
- public string ModelId { get; set; }
- public string Height { get; set; }
- public string Width { get; set; }
- public string Thickness { get; set; }
- public string Weight { get; set; }
- }
- }