Xml Dosyasından ListBox a Veri Ekleme

 

Merhaba arkadaşlar, bu makalemizde staff xml dosyamızdaki verileri listbox ta göstereceğiz. İlk önce using System.Xml yi ekleyelim. Xml dosyamızı aşağıdaki gibi hazırlıyoruz. 


Ben bu örnekte xml dosyasını projenin olduğu klasöre ekledim. Sizler xml dosyanınızı C:\ veya D:\ dizinine ekleyebilirsiniz.

Son olarak foreach kullanarak xml deki verileri listboxa aktarıyoruz.  

 


 










Şekil 1

 


 










Şekil 2

 


Form1.cs

 

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.IO;

using System.Linq;

 using System.Reflection;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

using System.Xml;

 

namespace listbox_xml {

 

public partial class Form1 : Form

 

{

 

public Form1() {

 

InitializeComponent(); }

 

private void btnDataBind_Click(object sender, EventArgs e) {

 

XmlDocument xmldoc = new XmlDocument();

FileStream fs = new FileStream(Path.Combine (Path.GetDirectoryName(Assembly.GetExecutingAssembly ().Location), "staff.xml"), FileMode.Open, FileAccess.Read);

//or

//FileStream fs = new FileStream(Path.Combine (Path.GetDirectoryName(Assembly.GetExecutingAssembly ().Location), @"D:\staff.xml"), FileMode.Open, FileAccess.Read);

 

xmldoc.Load(fs);

 

foreach (XmlNode node in xmldoc.DocumentElement) {

 

listBox1.Items.Add(node["id"].InnerText + "-" + node ["name"].InnerText + "-" + node["lastname"].InnerText + "-" + node["phone"].InnerText + "-" + node ["mail"].InnerText + "-" + node ["department"].InnerText);

}

 

 }

 

Bir makalenin daha sonuna geldik. Bir sonraki makalede görüşmek üzere. Bahadır ŞAHİN  

About Bahadır Şahin

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.

0 comments:

Öne Çıkan Yayın

GridView da Seçili Satırı DetailsView da Göstermek

Merhaba arkadaşlar bu makalemizde GridView nesnesi ile birlikte DetailsView nesanesini birlikte kullanacağız. GridView da seçili satırın de...