XPath

Dosyayıburadan indirebilirsiniz.

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.XPath;

namespace XPathOrnek
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        XPathDocument doc = null;
        XPathNavigator nav = null;
        XPathNodeIterator iterator = null;
        private void Form1_Load(object sender, EventArgs e)
        {
            doc = new XPathDocument("..\\..\\veriler.xml");
            nav = doc.CreateNavigator();

            iterator = nav.Select("Dukkan/Elektronik/Malzeme");
            XPathNodeIterator kucukIt = null;
            while (iterator.MoveNext())
            {
                ListViewItem lvi = new ListViewItem();

                kucukIt = iterator.Current.Select("Ad");
                kucukIt.MoveNext();
                lvi.Text = kucukIt.Current.Value;

                kucukIt = iterator.Current.Select("Fiyat");
                kucukIt.MoveNext();
                lvi.SubItems.Add(kucukIt.Current.Value);

                kucukIt = iterator.Current.Select("Stok");
                kucukIt.MoveNext();
                lvi.SubItems.Add(kucukIt.Current.Value);
                listView1.Items.Add(lvi);

            }
        }

        private void btnKacCesit_Click(object sender, EventArgs e)
        {
            string cesitSayisi = nav.Evaluate("count(Dukkan/Elektronik/Malzeme)").ToString();
            lblCesitSayisi.Text = cesitSayisi;
        }

        private void btnGetir_Click(object sender, EventArgs e)
        {
            lstSonuc.Items.Clear();
            // where Ad   = 'hasan'
            // where Stok > 3
            string deger = cmbSartTag.SelectedItem.ToString().ToLower().Equals("ad") ? "'" + txtDeger.Text + "'" : txtDeger.Text;

            iterator = nav.Select("Dukkan/Elektronik/Malzeme[" + cmbSartTag.SelectedItem.ToString() + cmbOperator.SelectedItem.ToString() + deger + "]");
            XPathNodeIterator kucukIt = null;
            while (iterator.MoveNext())
            {
                kucukIt = iterator.Current.Select(cmbTag.SelectedItem.ToString());
                kucukIt.MoveNext();
                lstSonuc.Items.Add(kucukIt.Current.Value);
            }
        }
    }
}

Bir cevap yazın

E-posta hesabınız yayımlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir