C# : Database monitoring system using XML file
By Perry
This application monitors the database of any kinds like employee, patients, or any items via XML. The XML file has a very nice interface to store the data, retrieve and update with O(1) time and hence it will be much faster as all the nodes will be unique. The search operation also use Hashing mechanism on XML node. This is very useful interface for any kind of application.
The complete windows form that I have used look like below:

Where I have used below windows components:private DataGrid dataGrid1; private GroupBox groupBox1; private TextBox TextBox1; private TextBox TextBox2; private TextBox TextBox3;
private TextBox TextBox4;
private TextBox TextBox5;
private RadioButton radioButton1; private RadioButton radioButton2; private Label label1; private Label label2; private Label label3;
private Label label4;
private Button button1; private Button button2; private Button button3;
You can get the InitializeComponent routine from here http://www.eggheadcafe.com/fileupload/-698781408_intialize_components.zip so that it will generate the similar form that I have given above. The XML file
that I have used in the project available at
http://www.eggheadcafe.com/fileupload/-698781408_company.zip
The output of application:

You can see that I have searched for Empid 101 and result have been displayed.
You can modify the XML file loaded into the text box and save it.
Rest of my code is below: You can copy paste it below the Initialization function:
[STAThread]
static void Main()
{
Application.Run(new EmployeeDatabase());
}
//Load the XML file into Text box on Form loading event
private void Form1_Load(object sender, EventArgs e)
{
if (e == null) throw new ArgumentNullException("e");
try
{
DataSet
ds = new DataSet();
ds.ReadXml(@"..\..\company.xml");
dataGrid1.DataSource
= ds.Tables[0];
XmlDataDocument
xd = new XmlDataDocument(ds);
if (xd.InnerXml != null) TextBox1.Text = xd.InnerXml;
}
catch (XmlException ex)
{
MessageBox.Show(ex.Message);
}
}
//Save the modified XML file when user sclicks Save button
private void button2_Click(object sender, EventArgs e)
{
if (e == null) throw new ArgumentNullException("e");
try
{
XmlTextReader
xr = new XmlTextReader(
new StringReader(TextBox1.Text));
DataSet
ds = new DataSet();
ds.ReadXml(xr);
XmlDataDocument
xd = new XmlDataDocument(ds);
ds.WriteXml(@"..\..\company.xml");
dataGrid1.DataSource
= xd.DataSet.Tables[0];
}
catch (XmlException ex)
{
MessageBox.Show(ex.Message);
}
}
//Allow only one radio button to be selected for searching based on Emp_ID
//or Emp_Name
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
if (e == null) throw new ArgumentNullException("e");
TextBox2.ReadOnly
= false;
TextBox3.ReadOnly
= true;
TextBox2.Focus();
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
if (e == null) throw new ArgumentNullException("e");
TextBox2.ReadOnly
= true;
TextBox3.ReadOnly
= false;
TextBox3.Focus();
}
// When user select any of radio button and clicks search button then search the
//XML file for required data and display
private void button1_Click(object sender, EventArgs e)
{
DataSet
ds = new DataSet();
ds.ReadXml(@"..\..\company.xml");
dataGrid1.DataSource
= ds.Tables[0];
XmlDataDocument
xd = new XmlDataDocument(ds);
if(button1.Text.Equals("Search"))
{
if (radioButton1.Checked)
{
XPathNavigator
xn = xd.CreateNavigator();
XPathExpression
expr = null;
try
{
if (xn != null)
expr
= xn.Compile(
"descendant::employees[id="+
TextBox2.Text
+ "]");
if (xn != null)
{
XPathNodeIterator
iterator = xn.Select(expr);
if(xn.Select(expr).Count == 0)
{
MessageBox.Show("Enter a valid employee Id number");
TextBox2.Text
="";
TextBox3.Text
="";
TextBox4.Text="";
button1.Text
="Search";
groupBox1.Text
= "Enter Search";
TextBox2.Focus();
return;
}
iterator.MoveNext();
XPathNavigator
nav2 = iterator.Current.Clone();
nav2.MoveToFirstChild();
TextBox2.Text
= nav2.Value;
nav2.MoveToNext();
TextBox3.Text
= nav2.Value;
nav2.MoveToNext();
TextBox4.Text
= nav2.Value;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
else if (radioButton2.Checked)
{
XPathNavigator
xn = xd.CreateNavigator();
XPathExpression
expr = null;
try
{
if (xn != null)
expr
= xn.Compile(
"descendant::employees[name='" +
TextBox3.Text
+ "']");
XPathNodeIterator
iterator = null;
if (xn != null)
{
iterator
= xn.Select(expr);
if(xn.Select(expr).Count == 0)
{
MessageBox.Show("Enter a valid employee name(case sensitive).");
TextBox2.Text
="";
TextBox3.Text
="";
TextBox4.Text="";
button1.Text
="Search";
groupBox1.Text
= "Searching String:";
TextBox3.Focus();
return;
}
}
if (iterator != null)
{
iterator.MoveNext();
XPathNavigator
nav2 = iterator.Current.Clone();
nav2.MoveToFirstChild();
TextBox2.Text
= nav2.Value;
nav2.MoveToNext();
TextBox3.Text
= nav2.Value;
nav2.MoveToNext();
TextBox4.Text
= nav2.Value;
}
}
catch (XPathException exp)
{
MessageBox.Show(exp.Message);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
button1.Text
= "Complete";
groupBox1.Text
= "Done";
}
else
{
TextBox2.Text
="";
TextBox3.Text
="";
TextBox4.Text="";
button1.Text
="Search";
groupBox1.Text
= "Search String:";
}
}
//When user modifies the textbos XML content then synchronize the changed XML in
//DataGrid
private void dataGrid1_CurrentCellChanged(object sender, EventArgs e)
{
if (e == null) throw new ArgumentNullException("e");
//This function syncronize the data in datagrid and XML
DataSet
ds = new DataSet();
ds.ReadXml(@"..\..\company.xml");
dataGrid1.DataSource
= ds.Tables[0];
XmlDataDocument
xd = new XmlDataDocument(ds);
TextBox1.Text
= xd.InnerXml;
}
private void button3_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
Popularity (1446 Views)
Article Discussion: C# : Database monitoring using XML file
Perry posted at Tuesday, December 09, 2008 1:42 AM