Sunday, 20 July 2014

Read XML File into DataSet and Populate in ComboBox Using C# in Windows Application

7/20/2014 - By Pranav Singh 0

This article will show you how you can read xml file in dataset and populate in combo box using c# in windows application. For reading the XML file I have used XmlTextReader  in c#.net.


Here is the XML file which we are going to bind

xml version="1.0" encoding="utf-8" ?>
<CountryDetail>
  <country>
    <Id>1</Id>
    <Name>India</Name>
  </country>
  <country>
    <Id>2</Id>
    <Name>Pakistan</Name>
  </country>
  <country>
    <Id>3</Id>
    <Name>America</Name>
  </country>
  <country>
    <Id>3</Id>
    <Name>Nepal</Name>
  </country>
</CountryDetail>

So for this article first we will create a new windows application and add a combobox in it. After this use the below code.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace XMLIncombobox
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            BindDropDown();
        }
        ///

        /// Read xml file and bind it to comboBox
        /// 
        public void BindDropDown()
        {
            /*Here for testing put your xml file in debuger folder*/
            XmlTextReader xmdatareader = new XmlTextReader("Xml/Country.xml");
            DataSet _objdataset = new DataSet();
            _objdataset.ReadXml(xmdatareader);
            comboBox1.DataSource = _objdataset.Tables[0];
        }
    }
}

       

In above code first we have read  the xml file first and after that we have stored it into dataset.   After storing into dataset we have passed to combobox as data source. As we run the code we will get the detail in data set as shown below.


Now here is the final output

                                

About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top