KONU : C Sharp ( C# ) xml write reader uygulaması , xml yazma ve okuma uygulama örnekleri, C Sharp xml document ,xmlnode, xml veri kaydetme, silme, güncelleme, c sharp xml veri silme, kaydetme, güncelleme uygulaması
UYGULAMAYI İNDİR
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
namespace XmlDocument_xmlnode_xml_veri_kaydetme_silme_guncelleme
{
public partial class Form1 : Form
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
public Form1()
{
InitializeComponent();
}
//bir dosyayı bellege almak için kullanılır
XmlDocument xmldocument = null;
void CalisanlariGetir()
{
//çalışanlar listesini temizle
lstb_calisanlar.Items.Clear();
foreach (XmlNode calisan in xmldocument.DocumentElement.ChildNodes)
{
if (calisan.Attributes[0].InnerText == null || calisan.Attributes[0].InnerText.Trim().Equals("")) continue;
lstb_calisanlar.Items.Add(calisan.ChildNodes[0].InnerText+" "+calisan.ChildNodes[1].InnerText);
}
}
string xmldocument_dosya = "Calisanlar.xml";
private void Form1_Load(object sender, EventArgs e)
{
// xmlDocument e yeni bir örnek oluşturulur.
xmldocument = new XmlDocument();
xmldocument.Load(xmldocument_dosya);
//Çalışanlar listesi doldurulur.
CalisanlariGetir();
}
//Bir çalışan güncellenmek istendiğinde çalışanın bilgileri bu node tutulacak
XmlNode guncellenecekNod = null;
private void btnKaydet_Click(object sender, EventArgs e)
{
if (guncellenecekNod == null)
{
//yeni bir kayıt yapılacak
//bir örnek node alınır
XmlNode yeninod = xmldocument.DocumentElement.ChildNodes[0].Clone();
//ID attribute degeri
yeninod.Attributes[0].InnerText = Guid.NewGuid().ToString();
//Ad degeri
yeninod.ChildNodes[0].InnerText = txtAd.Text;
//soyad degeri
yeninod.ChildNodes[1].InnerText = txtSoyad.Text;
yeninod.ChildNodes[2].InnerText = npdMaas.Value.ToString();
xmldocument.DocumentElement.AppendChild(yeninod);
CalisanlariGetir();
}
else
{
//guncelleme yapılacak
guncellenecekNod.ChildNodes[0].InnerText = txtAd.Text;
guncellenecekNod.ChildNodes[1].InnerText = txtSoyad.Text;
guncellenecekNod.ChildNodes[2].InnerText = npdMaas.Value.ToString();
xmldocument.Save(xmldocument_dosya);
xmldocument.Load(xmldocument_dosya);
CalisanlariGetir();
guncellenecekNod = null;
}
}
private void btnYazdir_Click(object sender, EventArgs e)
{
xmldocument.Save(xmldocument_dosya);
xmldocument.Load(xmldocument_dosya);//veriyi tekrar çektik
CalisanlariGetir();
}
private void listBox1_DoubleClick(object sender, EventArgs e)
{
// seçilen elemanın indexi 0 dan küçükse çık
if (lstb_calisanlar.SelectedIndex < 0)
return;
guncellenecekNod = xmldocument.DocumentElement.ChildNodes[lstb_calisanlar.SelectedIndex + 1];
txtAd.Text = guncellenecekNod.ChildNodes[0].InnerText;
txtSoyad.Text = guncellenecekNod.ChildNodes[1].InnerText;
npdMaas.Value = Convert.ToDecimal(guncellenecekNod.ChildNodes[2].InnerText);
}
private void silToolStripMenuItem_Click(object sender, EventArgs e)
{
int indeks = lstb_calisanlar.SelectedIndex;
if (indeks > -1)
{
DialogResult cevap = MessageBox.Show("Silmek istediğinize emin misiniz?", "Soru", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
if (cevap == DialogResult.Yes)
{
xmldocument.DocumentElement.RemoveChild(xmldocument.DocumentElement.ChildNodes[indeks + 1]);
xmldocument.Save(xmldocument_dosya);
xmldocument.Load(xmldocument_dosya);
CalisanlariGetir();
txtAd.Text = "";
txtSoyad.Text = "";
npdMaas.Value = 0;
}
}
}
}
}

Hiç yorum yok :
Yorum Gönder