Click here to Skip to main content
15,886,362 members
Articles / Programming Languages / XML

Create Data Classes

Rate me:
Please Sign up or sign in to vote.
4.88/5 (31 votes)
4 Mar 2011CPOL10 min read 135.4K   2.5K   167  
An application that creates a C# class to read/write data to/from an Access, SQLite, or XML database.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;

/*
 * Copyright Jeff Gaines 2007 - 2011 (jeff@jgaines.co.uk)
 * Free licence for private use
 * Please contact the author if you wish to use the code commercially
*/

namespace JGCreateDataClasses
{
	/// <summary>
	/// Form for reading in an XML database schema
	/// </summary>
	internal partial class FormReadXMLSchema : Form
	{
		private List<string> m_TableNamesList = new List<string>();

		private ListView m_ListView;

		private string m_XMLTableName = "";

		//	Tooltip for buttons
		private ToolTip m_ToolTip = new ToolTip();

		internal FormReadXMLSchema(ListView listView)
		{
			InitializeComponent();
			this.m_ListView = listView;

			//	ToolTips
			m_ToolTip.SetToolTip(btnClose, "Close Dialog");
			m_ToolTip.SetToolTip(btnReadSchema, "Read XML Schema");

			SetFileFilter();
		}

		private void FormReadXMLSchema_Load(object sender, EventArgs e)
		{
			jFileFolderPicker1.Focus();
		}

		private void btnClose_Click(object sender, EventArgs e)
		{
			Close();
		}

		private void btnReadSchema_Click(object sender, EventArgs e)
		{
			if (!string.IsNullOrEmpty(jFileFolderPicker1.SelectedPath))
				ReadSchema(jFileFolderPicker1.SelectedPath);
		}

		private void jFileFolderPicker1_OnPathSelectedHandler(object sender, JGFileFolderPicker.PathArgs e)
		{
			if (!string.IsNullOrEmpty(e.SelectedPath))
			{
				try
				{
					FileInfo fInfo = new FileInfo(e.SelectedPath);
					if (fInfo.Exists)
					{
						ListSchemaTables(fInfo.FullName);
					}
				}
				catch
				{
					ShowMessage("Cannot Open File", true);
				}
			}
		}

		private void ListSchemaTables(string filePath)
		{
			try
			{
				DataSet dsTemp = new DataSet();
				dsTemp.ReadXmlSchema(filePath);

				txtXMLNamespace.Text = dsTemp.Namespace;
				txtXMLDataSetName.Text = dsTemp.DataSetName;

				m_TableNamesList.Clear();
				DataTableCollection dtCollection = dsTemp.Tables;
				foreach (DataTable current in dtCollection)
				{
					lstTables.Items.Add(current.TableName);
					m_TableNamesList.Add(current.TableName);
				}
			}
			catch
			{
				ShowMessage("Unable To Read Schema", true);
			}
		}

		private void lstTables_SelectedIndexChanged(object sender, EventArgs e)
		{
			btnReadSchema.Enabled = (lstTables.SelectedItems.Count == 1);
		}

		private void ReadSchema(string filePath)
		{
			int tableIndex = lstTables.SelectedIndex;
			
			ListViewItem lvItem;
			
			string accessType = "", cType = "";
			
			if (rbReplaceData.Checked)
				this.m_ListView.Items.Clear();
			
			ShowMessage("", false);

			try
			{
				DataSet dsTemp = new DataSet();
				dsTemp.ReadXmlSchema(filePath);

				DataTableCollection dtCollection = dsTemp.Tables;
				DataTable dTable = dtCollection[tableIndex];
				DataColumnCollection dcCollection = dTable.Columns;
				this.m_XMLTableName = dTable.TableName;

				foreach (DataColumn column in dcCollection)
				{
					lvItem = new ListViewItem(column.ColumnName);

					for (int col = 0; col <= this.m_ListView.Columns.Count; col++)
						lvItem.SubItems.Add("");

					this.m_ListView.Items.Add(lvItem);

					JDataTypes.GetTypeStringsFromXMLType(column.DataType.ToString(), out cType, out accessType);

					lvItem.SubItems[JCommon.CDATATYPE].Text = cType;
					lvItem.SubItems[JCommon.ACCESSDATATYPE].Text = accessType;
					lvItem.SubItems[JCommon.XMLDATATYPE].Text = column.DataType.ToString();
					if (column.AutoIncrement)
					{
						lvItem.SubItems[JCommon.AUTOINCREMENT].Text = "Yes";
						lvItem.SubItems[JCommon.KEYFIELD].Text = "Yes";
					}
					else
					{
						lvItem.SubItems[JCommon.AUTOINCREMENT].Text = "";
						lvItem.SubItems[JCommon.KEYFIELD].Text = "";
					}

					lvItem.SubItems[JCommon.STRINGOVERRIDE].Text = "";
					lvItem.SubItems[JCommon.OWNGET].Text = "";
					lvItem.SubItems[JCommon.OWNUPDATER].Text = "";
				}
			}
			catch
			{
				ShowMessage("Unable To Read Schema", true);
			}
		}

		private void SetFileFilter()
		{
			string filter = "XML Files (*.xml;*.xsd)|*.xml;*.xsd";

			jFileFolderPicker1.FileFilter = filter;
		}

		private void ShowMessage(string strMes, bool blnBeep)
		{
			tslblMessage.Text = strMes;
			if (blnBeep)
				JCommon.MessageBeep(-1);
		}


		#region internal properties

		internal List<string> TableNamesList
		{
			get { return this.m_TableNamesList; }
		}

		internal string XMLFilePath
		{
			get { return this.jFileFolderPicker1.SelectedPath; }
		}

		internal string XMLTableName
		{
			get { return this.m_XMLTableName; }
		}

		internal string XMLNameSpace
		{
			get { return this.txtXMLNamespace.Text; }
		}


		internal string XMLDataSetName
		{
			get { return this.txtXMLDataSetName.Text; }
		}


		#endregion

		

		



	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Retired
United Kingdom United Kingdom
I have been a keen hobbyist programmer since getting my first computer - a Vic 20 (you had to be able to write programs then since few programs were available and all were expensive).
Retired and now living in Pewsey, Wiltshire, where I spend (far too much of) my time writing computer programs to keep my mind active.

Comments and Discussions