C# Book Store(Csharp kitabevi)

21. May 2010

Read this article in your language IT | EN | DE | ES

Merhaba  arkadaşlar   uzun bir aradan sonra tekrar  beraberiz bu  örnek projemde sizlere  c# üzerinde  xml  yapısının  kullanrak bir book store projesi   nasıl  yaparız onu görelim  örnek projemde 
2 adet windows forms  bulunuyor  birinci form da genel işlevler  bulunmakta  2 form ise kayit xml  verilerimi bu  form üzerinde  listeletecem örnek projemizin gerekli  kodlarını  yazalım

using System;
using System.Windows.Forms;
using System.Xml;
using BookStore.Properties;

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

        readonly XmlDocument _doc = new XmlDocument();

        private void BtnLoadXmlClick(object sender, EventArgs e)
        {
            var result = openFileDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                _doc.Load(openFileDialog1.FileName);
            }
        }

        private void BtnViewXmlClick(object sender, EventArgs e)
        {
            var previewForm = new Form2 { textBox1 = { Text = _doc.OuterXml } };
            previewForm.ShowDialog();
        }

        int _chapterCounter = 1;

        private void BtnAddChapterClick(object sender, EventArgs e)
        {
            if (txtChapterName.Text != "")
            {
                var anItem = new ListViewItem(new[] { _chapterCounter.ToString(), txtChapterName.Text, txtChapterText.Text });
                listView1.Items.Add(anItem);
                _chapterCounter++;
            }
        }

        private void BtnAddBookClick(object sender, EventArgs e)
        {
            if (txtISBN.Text != "" & txtTitle.Text != "" & txtPrice.Text != "")
            {
                if (_doc.OuterXml == "")
                {
                    MessageBox.Show(Resources.Form1_BtnAddBookClick_Lütfen_kitap_xmli_yükleyin);
                    return;
                }

                var aList = _doc.GetElementsByTagName("book");
                var aNode = aList.Item(aList.Count - 1);

                var anElement = _doc.CreateElement("book");
                anElement.SetAttribute("ISBN", txtISBN.Text);
                anElement.SetAttribute("Title", txtTitle.Text);
                anElement.SetAttribute("Price", txtPrice.Text);

                if (listView1.Items.Count != 0)
                {
                    XmlElement bElement;

                    foreach (ListViewItem listViewItem in listView1.Items)
                    {
                        bElement = _doc.CreateElement("chapter");
                        bElement.SetAttribute("num", listViewItem.SubItems[0].Text);
                        bElement.SetAttribute("name", listViewItem.SubItems[1].Text);
                        var aText = _doc.CreateTextNode(listViewItem.SubItems[2].Text);
                        bElement.AppendChild(aText);
                        anElement.AppendChild(bElement);
                    }
                }

                aNode.ParentNode.InsertAfter(anElement, aNode);
                txtChapterName.Text = "";
                txtChapterText.Text = "";
                txtISBN.Text = "";
                txtPrice.Text = "";
                txtTitle.Text = "";
                listView1.Items.Clear();
            }
            else
            {
                MessageBox.Show(Resources.Form1_BtnAddBookClick_lütfen_tüm_alanları_doldurun);
            }
        }

        private void BtnSaveXmlClick(object sender, EventArgs e)
        {
            var writer = XmlWriter.Create("bookStore2.xml");
            if (writer != null) _doc.Save(writer);
        }
    }
}
Xml veri tabanım
<?xml version="1.0" encoding="utf-8" ?>
<Bookstore>
    <genre name="İş">
        <book ISBN="BU1233" title="deneme" price="19.99">
            <chapter num="1" name="Giriş">Açıklama var....</chapter>
            <chapter num="2" name="Gelişme">Açıklama var....</chapter>
            <chapter num="3" name="Sonuc">Açıklama var....</chapter>     
        </book>
        <book ISBN="BU1643" title="deneme1" price="29.99">
            <chapter num="1" name="Giriş">Açıklama var....</chapter>
            <chapter num="2" name="Gelişme">Açıklama var....</chapter>
            <chapter num="3" name="Sonuc">Açıklama var....</chapter>
        </book>
        <book ISBN="BU1233" title="deneme2" price="55.99">
            <chapter num="1" name="Giriş">Açıklama var....</chapter>
            <chapter num="2" name="Gelişme">Açıklama var....</chapter>
            <chapter num="3" name="Sonuc">Açıklama var....</chapter>
        </book>
    </genre>
</Bookstore>



Evet Arkadaşlar örnek bir projenin  sonuna gelmiş olduk  bu projemde sizlere  bir windows  yapısının   genel  kullanımı  oluşturdum  başka bir örnek projede buluşmak üzere herkeze  iyi calışmalar kolay gelsin
Saygılarımla Orhan Türk

FacebookDigg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

C# , Tüm Projelerim , ,

Comments

5/27/2010 10:15:40 PM #
Hey there, just stumbled upon your blog through Yahoo, and found it to be really detailed. Im gonna keep an eye on this one. Cheers!
5/29/2010 10:59:22 AM #

Add comment


(Will show your Gravatar icon)

biuquote
  • Comment
  • Preview
Loading



Sayfa 0.015625 saniyede yuklendi.