This tutorial will show you how to develop a voting/polling system in a Windows Form with the use of XML and LINQ, using VB.

Polling systems can be extremely useful for capturing opinions or current trends. In this tutorial, we will look at how we can create a Windows Form to build a voting system where users can vote who is their favorite Presidentail Candidate, and then see the current results. We will be using an XML file to store the data. We will start by creating the structure of the XML file:

Next, we can design our Form; we will have a label citing the question, and then the two options as Radio Buttons, and then a button to submit the vote. All of these controls can be grouped together. And then underneath, we can put another button to show current results of the poll, and a label to output the results.

Once we have designed our form, we can double-click on the Submit button to create a handler for it in the code-behind:

On button click, we check to see which option has been chosen, and then we call a method to commit the vote to the XML file. The submitVote method looks something like this:

Here, we are making use of a Try..Catch to try and reduce errors shown to the user. We are then opening the XML document and using LINQ to add a new element to it, saving the newly-cast vote into the document. Finally, we call the readXML method to output current results to the form:

This method also makes use of LINQ to select all data from the XML file and then loop through it to count all votes for each candidate. Then we perform a simple calculation to determine the percentage of votes for each. Finally, we output the results to the form.
The entire code-behind will look something like this:

Download Source Files