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. namespace WindowsFormsApplication1
  7. {
  8. public partial class Form1 : Form
  9. {
  10. private const string FilePath = @"C:\xmlfile.xml";
  11. public Form1()
  12. {
  13. InitializeComponent();
  14. }
  15. /// <summary>
  16. /// Save Values in XML
  17. /// </summary>
  18. /// <param name="sender"></param>
  19. /// <param name="e"></param>
  20. private void btnSaveValuesInXML_Click(object sender, EventArgs e)
  21. {
  22. // Creates an instance of the System.Xml.XmlTextWriter class using the specified file.
  23. var textWriter = new XmlTextWriter(FilePath, null);
  24. // Writes the XML declaration with the version "1.0".
  25. textWriter.WriteStartDocument();
  26. // Write comments
  27. textWriter.WriteComment("First Comment for XmlTextWriter Sample Example");
  28. // Write first element
  29. textWriter.WriteStartElement("Student");
  30. // textWriter.WriteStartElement("Document", "Test", "RECORD");
  31. // Write next element Name
  32. textWriter.WriteStartElement("Name");
  33. textWriter.WriteString(textBox1.Text);
  34. textWriter.WriteEndElement();
  35. // Write element Address
  36. textWriter.WriteStartElement("Address");
  37. textWriter.WriteString(textBox2.Text);
  38. textWriter.WriteEndElement();
  39. // Write element Pincode
  40. textWriter.WriteStartElement("Pincode");
  41. textWriter.WriteString(textBox3.Text);
  42. textWriter.WriteEndElement();
  43. // Ends the document.
  44. textWriter.WriteEndDocument();
  45. // close writer
  46. textWriter.Close();
  47. MessageBox.Show(@"Values got stored in XML");
  48. }
  49. /// <summary>
  50. /// Retrieve Items
  51. /// </summary>
  52. /// <param name="sender"></param>
  53. /// <param name="e"></param>
  54. private void btnRetrieveItems_Click(object sender, EventArgs e)
  55. {
  56. var doc = new XmlDocument();
  57. doc.Load(FilePath);
  58. var nodeNames = new List<string>();
  59. var xmlNodeList = doc.SelectNodes("/Student");
  60. // Normal Foreach Statement.
  61. //if (xmlNodeList != null)
  62. // foreach (System.Xml.XmlNode node in xmlNodeList)
  63. // {
  64. // foreach (System.Xml.XmlNode child in node.ChildNodes)
  65. // {
  66. // nodeNames.Add(child.InnerText);
  67. // }
  68. // }
  69. // Linq Statement
  70. if (xmlNodeList != null)
  71. nodeNames.AddRange(from XmlNode node in xmlNodeList
  72. from XmlNode child in node.ChildNodes
  73. select child.InnerText);
  74. foreach (var node in nodeNames)
  75. {
  76. listView1.Items.Add(node); // You can add SubItems for this also.
  77. listBox1.Items.Add(node);
  78. }
  79. }
  80. }
  81. }