reading an xml file using c#.net (including images)   Leave a comment


using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.VisualBasic;

namespace xmlapp
{  public partial class studdata : Form
{   public studdata()
{ InitializeComponent(); }
DataSet ds;  string dir;  int rno = 0;
private void studdata_Load(object sender, EventArgs e)
{ ds = new DataSet();
searchbtn.Enabled = firstbtn.Enabled = prevbtn.Enabled = nextbtn.Enabled = lastbtn.Enabled =clearbtn.Enabled= false;
}
private void loadbtn_Click(object sender, EventArgs e)
{ try
{  searchbtn.Enabled = firstbtn.Enabled = prevbtn.Enabled = nextbtn.Enabled = lastbtn.Enabled = clearbtn.Enabled = false;
ds.Clear(); //for clearing grid view
dataGridView1.DataSource = null;
sno_txtbx.Text = sname_txtbx.Text = course_txtbx.Text = fee_txtbx.Text = “”;
pictureBox1.Image = null;
openFileDialog1.Filter = “xml|*.xml|all files|*.*”;
DialogResult res = openFileDialog1.ShowDialog();
if (res == DialogResult.OK)
{  string fpath = openFileDialog1.FileName;
ds.ReadXml(fpath);
//setting primary key inorder to search a record using finding method
ds.Tables[0].Constraints.Add(“pk_sno”, ds.Tables[0].Columns[0], true);
dataGridView1.DataSource = ds.Tables[0];
dir = fpath.Substring(0, fpath.LastIndexOf(“\\”) + 1);
showdata();
}
}
catch
{ MessageBox.Show(“invalid input”);  }
}
void showdata()
{  if (ds.Tables[0].Rows.Count > 0)
{  searchbtn.Enabled = firstbtn.Enabled = prevbtn.Enabled = nextbtn.Enabled = lastbtn.Enabled = clearbtn.Enabled = true;
pictureBox1.Image = null;
sno_txtbx.Text = ds.Tables[0].Rows[rno][0].ToString();
sname_txtbx.Text = ds.Tables[0].Rows[rno][1].ToString();
course_txtbx.Text = ds.Tables[0].Rows[rno][2].ToString();
fee_txtbx.Text = ds.Tables[0].Rows[rno][3].ToString();
pictureBox1.ImageLocation = dir + ds.Tables[0].Rows[rno][4].ToString();
}
else
MessageBox.Show(“No records”);
}
private void searchbtn_Click(object sender, EventArgs e)
{     int n = Convert.ToInt32(Interaction.InputBox(“Enter sno to search:”, “Search”, “10”, 200, 200));
////manual searching
// int i, j, flag = 0;
//for (i = 0; i <= ds.Tables[0].Rows.Count – 1; i++)
//{  //    j = Convert.ToInt32(ds.Tables[0].Rows[i][0]);
//    if (n == j)  {rno = i;    flag = 1;    showdata();   }
//}
//if (flag != 1){ MessageBox.Show(“record not found”); }

//searching using find method
DataRow drow = ds.Tables[0].Rows.Find(n);
if (drow != null)
{  rno= ds.Tables[0].Rows.IndexOf(drow);
sno_txtbx.Text = drow[0].ToString();
sname_txtbx.Text = drow[1].ToString();
course_txtbx.Text = drow[2].ToString();
fee_txtbx.Text = drow[3].ToString();
pictureBox1.ImageLocation = dir + drow[4];
}
else
MessageBox.Show(“record not found”);
}
private void clearbtn_Click(object sender, EventArgs e)
{  sno_txtbx.Text=sname_txtbx.Text =course_txtbx.Text =fee_txtbx.Text = “”;
pictureBox1.Image = null;
}
private void exitbtn_Click(object sender, EventArgs e)
{   Application.Exit();     }
private void firstbtn_Click(object sender, EventArgs e)
{      rno = 0;
showdata();
}
private void prevbtn_Click(object sender, EventArgs e)
{ if (rno > 0)
{  rno–;
showdata();
}
else
{ MessageBox.Show(“first record”); }
}
private void nextbtn_Click(object sender, EventArgs e)
{ if (rno < ds.Tables[0].Rows.Count – 1)
{   rno++;
showdata();
}
else
{ MessageBox.Show(“LastRecord”); }
}
private void lastbtn_Click(object sender, EventArgs e)
{      rno = ds.Tables[0].Rows.Count – 1;
showdata();
}
}
}

——————————

——————————————————–
example for creating an xml file:
Open notepad and type the following & save it with extension .xml
——————————-
<students>
<student>
<sno>10</sno>
<sname>Prashanth</sname>
<course>dotnet</course>
<fee>3500</fee>
<photo>prash.jpg</photo>
</student>
<student>
<sno>20</sno>
<sname>Satyapal</sname>
<course>java</course>
<fee>3000</fee>
<photo>satya.jpg</photo>
</student>
<student>
<sno>30</sno>
<sname>Mahender</sname>
<course>php</course>
<fee>2500</fee>
<photo>mahi.jpg</photo>
</student>
</students>
——————————
Note: xml file and respective jpeg images should be in same folder.

->To execute program click loadxml button and load xml file

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: