Read and Write XML File using XMLDocument

Read & Write XML File using XMLDocument

Write XML File Using Xml Writer,
  1. public void WriteXMLFileUsingXmlWriter()  
  2. {  
  3.     List < LumiaMobile > LumiaMobiles = new List < LumiaMobile > ();  
  4.     LumiaMobiles.Add(new LumiaMobile  
  5.     {  
  6.         ModelId = "lumia550",  
  7.             Height = "136.1 mm",  
  8.             Width = "67.8 mm",  
  9.             Thickness = "9.9 mm",  
  10.             Weight = "141.9 g"  
  11.     });  
  12.     LumiaMobiles.Add(new LumiaMobile  
  13.     {  
  14.         ModelId = "lumia950",  
  15.             Height = "145 mm",  
  16.             Width = "73.2 mm",  
  17.             Thickness = "8.2 mm",  
  18.             Weight = "150 g"  
  19.     });  
  20.     LumiaMobiles.Add(new LumiaMobile  
  21.     {  
  22.         ModelId = "Text",  
  23.             Height = "",  
  24.             Width = "",  
  25.             Thickness = "8.2 mm",  
  26.             Weight = "150 g"  
  27.     });  
  28.     using(XmlWriter writer = XmlWriter.Create("LumiaMobiles.xml"))  
  29.     {  
  30.         writer.WriteStartDocument();  
  31.         writer.WriteStartElement("LumiaMobiles");  
  32.         foreach(LumiaMobile lumiaMobile in LumiaMobiles)  
  33.         {  
  34.             writer.WriteStartElement(lumiaMobile.ModelId);  
  35.             writer.WriteElementString("Height", lumiaMobile.Height);  
  36.             writer.WriteElementString("Width", lumiaMobile.Width);  
  37.             writer.WriteElementString("Thickness", lumiaMobile.Thickness);  
  38.             writer.WriteElementString("Weight", lumiaMobile.Weight);  
  39.             writer.WriteEndElement();  
  40.         }  
  41.         writer.WriteEndElement();  
  42.         writer.WriteEndDocument();  
  43.     }  
  44. }  
The above code snippet will write XML file in single line i.e. not formatted, all content in same line. 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2.     <LumiaMobiles>  
  3.         <lumia550>  
  4.             <Height>136.1 mm</Height>  
  5.             <Width>67.8 mm</Width>  
  6.             <Thickness>9.9 mm</Thickness>  
  7.             <Weight>141.9 g</Weight>  
  8.         </lumia550>  
  9.         <lumia950>  
  10.             <Height>145 mm</Height>  
  11.             <Width>73.2 mm</Width>  
  12.             <Thickness>8.2 mm</Thickness>  
  13.             <Weight>150 g</Weight>  
  14.         </lumia950>  
  15.         <Text>  
  16.             <Height />  
  17.             <Width />  
  18.             <Thickness>8.2 mm</Thickness>  
  19.             <Weight>150 g</Weight>  
  20.         </Text>  
  21.     </LumiaMobiles>  
So to write formatted XML data we can use XmlTextWriter,
 
Write XML File Using Xml TextWriter,
  1. public void WriteXMLFileUsingXmlTextWriter()  
  2. {  
  3.     List < LumiaMobile > LumiaMobiles = new List < LumiaMobile > ();  
  4.     LumiaMobiles.Add(new LumiaMobile  
  5.     {  
  6.         ModelId = "lumia550",  
  7.             Height = "136.1 mm",  
  8.             Width = "67.8 mm",  
  9.             Thickness = "9.9 mm",  
  10.             Weight = "141.9 g"  
  11.     });  
  12.     LumiaMobiles.Add(new LumiaMobile  
  13.     {  
  14.         ModelId = "lumia950",  
  15.             Height = "145 mm",  
  16.             Width = "73.2 mm",  
  17.             Thickness = "8.2 mm",  
  18.             Weight = "150 g"  
  19.     });  
  20.     LumiaMobiles.Add(new LumiaMobile  
  21.     {  
  22.         ModelId = "Text",  
  23.             Height = "",  
  24.             Width = "",  
  25.             Thickness = "8.2 mm",  
  26.             Weight = "150 g"  
  27.     });  
  28.     string filename = "LumiaMobiles.xml";  
  29.     using(XmlTextWriter writer = new XmlTextWriter(new StreamWriter(new FileStream(filename, FileMode.Create))))  
  30.     {  
  31.         writer.Formatting = Formatting.Indented;  
  32.         writer.Indentation = 4;  
  33.         writer.WriteStartDocument();  
  34.         writer.WriteStartElement("LumiaMobiles");  
  35.         foreach(LumiaMobile lumiaMobile in LumiaMobiles)  
  36.         {  
  37.             writer.WriteStartElement(lumiaMobile.ModelId);  
  38.             writer.WriteElementString("Height", lumiaMobile.Height);  
  39.             writer.WriteElementString("Width", lumiaMobile.Width);  
  40.             writer.WriteElementString("Thickness", lumiaMobile.Thickness);  
  41.             writer.WriteElementString("Weight", lumiaMobile.Weight);  
  42.             writer.WriteEndElement();  
  43.         }  
  44.         writer.WriteEndElement();  
  45.         writer.WriteEndDocument();  
  46.     }  
  47. }  
It will write XML file as: 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LumiaMobiles>  
  3.     <lumia550>  
  4.         <Height>136.1 mm</Height>  
  5.         <Width>67.8 mm</Width>  
  6.         <Thickness>9.9 mm</Thickness>  
  7.         <Weight>141.9 g</Weight>  
  8.     </lumia550>  
  9.     <lumia950>  
  10.         <Height>145 mm</Height>  
  11.         <Width>73.2 mm</Width>  
  12.         <Thickness>8.2 mm</Thickness>  
  13.         <Weight>150 g</Weight>  
  14.     </lumia950>  
  15.     <Text>  
  16.         <Height />  
  17.         <Width />  
  18.         <Thickness>8.2 mm</Thickness>  
  19.         <Weight>150 g</Weight>  
  20.     </Text>  
  21. </LumiaMobiles>   
Now use the below code to Read the above XML File,
  1. public void ReadXMLFile(string xmlFileName)  
  2. {  
  3.     XmlDocument xmlDocument = new XmlDocument();  
  4.     xmlDocument.Load(xmlFileName);  
  5.     XmlNodeList nodeList = xmlDocument.SelectNodes("LumiaMobiles");  
  6.     foreach(XmlNode node in nodeList)  
  7.     {  
  8.         XmlNodeList lumianodes = node.ChildNodes;  
  9.         foreach(XmlNode item in lumianodes)  
  10.         {  
  11.             Console.WriteLine("ModelID : {0}", item.Name);  
  12.             Console.WriteLine("Height: {0}", item.SelectSingleNode("Height").InnerText);  
  13.             Console.WriteLine("Width: {0}", item.SelectSingleNode("Width").InnerText);  
  14.             Console.WriteLine("Thickness: {0}", item.SelectSingleNode("Thickness").InnerText);  
  15.             Console.WriteLine("Weight: {0}", item.SelectSingleNode("Weight").InnerText);  
  16.             Console.WriteLine();  
  17.         }  
  18.     }  
  19.     Console.ReadKey();  
  20. }