How to read and write XML

 Mar 16, 2015

We create applications on a daily basis to meet our customers’ requirements; often the case will be to create a solution that can retrieve some information out of a data source, such as a database or an XML file.

When we create a solution that uses XML, not only can we save information about our entities, for example products or customers, but we can also save our applications configurations into the file.

By doing this, you can make sure that when the user loads the application you can apply these settings to the application, without the user needing to set it up again. To do this we can create an XML file.

To perform this, will have the following controls on your form:


  1. A ComboBox named cboxcolor
  2. A NumericUpDown named numwidth
  3. A NumericUpDown named numheight
  4. A Button named btnSaveconfig
  5. A few labels to describe the controls

By performing the following steps, we will create an XML file to save the configuration settings and then read the settings upon loading the form. (Below is the full XML file.)


  1. Make sure you reference the needed namespaces by adding it on top of your code System.XM
  2. Read-write-XML-3

  3. First create a Method called applysetting to apply all the changes you made.
  4. Read-write-XML-4
  5. Create a click event for the btnSaveconfig button,. This event will be used to write the XML file that contains the configurations settings and then apply it to the form.
  6. Add the following code to the btnSaveconfig click event. Read the notes on the code script for the explanations (prefixed with //.)
  7. Read-write-XML-5
  8. Finally we need to load the settings from the XML file when the application starts up again.
  9. Read-write-XML-6
  10. With all this in place you should now have an application that can save an XML file with configuration in the bin directory of your solution and also, read from that file to apply the given settings.
  11. How do your Excel skills stack up?   

    Test Now  

How do
your Excel skills
stack up?

Grade your skills now

About the Author:

Auret Swanepoel  

As a recent addition to the New Horizons team, Auret is a highly skilled and qualified IT Technical trainer. He has been a Microsoft Certified Trainer (MCT) since 2008 and has since then, also become a Microsoft Certified Professional (MCP), a Microsoft Certified Technology Specialist (MCTS) and a Microsoft Certified Information Technology Professional (MCITP). With his international experience as a trainer in South Africa, Auret is able to adapt his teaching style to different audiences in the classroom and ensure that students are learning in a positive and collaborative environment.

Read full bio
Back to top