This tutorial will show how we can add data to an XML file using WinForms. C# version.

WinForms are the Windows Applications we can create in Visual Studio. In this tutorial, we will explore how we can use LINQ to add data to an XML form, and then how we can extract this data and display it on our form.

The first thing that we will do is design our form. We will have three textboxes, two buttons and a Rich Text Box. We will use the textboxes to input data to be added to the XML file, and then one button to submit the data to be added and the other button to display the XML file in the RichTextBox.

The form will look something like this:

Once we have our form designed, we can start programming the buttons. On the first button, we will need to create an XML document with the data provided in the textboxes. We will use the following code:

This block of code creates a new XML structure for the textbox data. In this example, we have Name, City and Age. The XML output of this code will look something like this:

The other button will display the XML file once we have added to it. For this button, we can use the following code:

This method loads the XML file and then checks for the Name, City and Age elements. It returns all the data in the XML file and outputs it to the RichTextBox. The entire code-behind for this application is as follows:

Download Source Files