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.
Some of my previous articles are as follows: How
to Create Column Chart in Windows Application Using C#.Net, Windows
Application - Excel Sheet Name in C#.Net, Bind
And Display Image in a DatagridView Using C#.Net in Windows Application, WaterMark
TextBox In Windows Applications Using C# and VB.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
0 comments:
Please let me know your view