Saving Values in XML and Retrieving Them

As I saw So many new developers are asking similar question so thought of writing this blog.
  
So to made it bit user friendly, I put 3 text box, where you can enter the values and by clicking "Save value in XML" button, you can save the values in XML format. Currently I am using @"C:\xmlfile.xml" for file path, so you can modify this as per your requirement. I have also added 1 list view and 1 list box, where you can get the values from XML by clicking "RetrieveItems" button. 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Windows.Forms;  
  5. using System.Xml;  
  6.   
  7. namespace WindowsFormsApplication1  
  8. {  
  9.     public partial class Form1 : Form  
  10.     {  
  11.         private const string FilePath = @"C:\xmlfile.xml";  
  12.   
  13.         public Form1()  
  14.         {  
  15.             InitializeComponent();  
  16.         }  
  17.   
  18.         /// <summary>  
  19.         /// Save Values in XML  
  20.         /// </summary>  
  21.         /// <param name="sender"></param>  
  22.         /// <param name="e"></param>  
  23.         private void btnSaveValuesInXML_Click(object sender, EventArgs e)  
  24.         {  
  25.             // Creates an instance of the System.Xml.XmlTextWriter class using the specified file.     
  26.             var textWriter = new XmlTextWriter(FilePath, null);  
  27.             // Writes the XML declaration with the version "1.0".     
  28.             textWriter.WriteStartDocument();  
  29.             // Write comments     
  30.             textWriter.WriteComment("First Comment for XmlTextWriter Sample Example");  
  31.             // Write first element     
  32.             textWriter.WriteStartElement("Student");  
  33.             // textWriter.WriteStartElement("Document", "Test", "RECORD");  
  34.             // Write next element Name     
  35.             textWriter.WriteStartElement("Name");  
  36.             textWriter.WriteString(textBox1.Text);  
  37.             textWriter.WriteEndElement();  
  38.             // Write element Address     
  39.             textWriter.WriteStartElement("Address");  
  40.             textWriter.WriteString(textBox2.Text);  
  41.             textWriter.WriteEndElement();  
  42.             // Write element Pincode     
  43.             textWriter.WriteStartElement("Pincode");  
  44.             textWriter.WriteString(textBox3.Text);  
  45.             textWriter.WriteEndElement();  
  46.             // Ends the document.     
  47.             textWriter.WriteEndDocument();  
  48.             // close writer     
  49.             textWriter.Close();  
  50.             MessageBox.Show(@"Values got stored in XML");  
  51.         }  
  52.   
  53.         /// <summary>  
  54.         /// Retrieve Items  
  55.         /// </summary>  
  56.         /// <param name="sender"></param>  
  57.         /// <param name="e"></param>  
  58.         private void btnRetrieveItems_Click(object sender, EventArgs e)  
  59.         {  
  60.             var doc = new XmlDocument();  
  61.             doc.Load(FilePath);  
  62.   
  63.             var nodeNames = new List<string>();  
  64.             var xmlNodeList = doc.SelectNodes("/Student");  
  65.               
  66.             // Normal Foreach Statement.  
  67.             //if (xmlNodeList != null)  
  68.             //    foreach (System.Xml.XmlNode node in xmlNodeList)  
  69.             //    {  
  70.             //        foreach (System.Xml.XmlNode child in node.ChildNodes)  
  71.             //        {  
  72.             //            nodeNames.Add(child.InnerText);  
  73.             //        }  
  74.             //    }  
  75.   
  76.             // Linq Statement  
  77.             if (xmlNodeList != null)  
  78.                 nodeNames.AddRange(from XmlNode node in xmlNodeList  
  79.                     from XmlNode child in node.ChildNodes  
  80.                     select child.InnerText);  
  81.   
  82.             foreach (var node in nodeNames)  
  83.             {  
  84.                 listView1.Items.Add(node); // You can add SubItems for this also.  
  85.                 listBox1.Items.Add(node);  
  86.             }  
  87.   
  88.         }  
  89.     }  
  90. }  
 
Ebook Download
View all
Learn
View all