In this article I am going to explain how to use XSD Dataset to read and write data in xml format using c# in win forms.
With Ado.NET you can fill a Dataset from an XML stream or document. You can use the XML stream or document to supply the Dataset either data, schema information, or both. The information supplied from the XML stream or document can be combined with existing data or schema information already present in the DataSet.
The following example demonstrates how to create a Customer Entry Form using XSD dataset. We are saving customer data in xml file (Customer.xml) and also using linq to find the records of customer.
Before creating the following application, do the following steps:
· Create a new Windows application using Microsoft Visual C#.
· Design a Customer entry form as shown in below screenshot:
CustomerID field is read-only because it will be auto generated.
Add some default countries in the Country combobox, as given below:
Design a Customer Search form
· Make Done button DialogResult property to OK and Cancel button DialogResult property to Cancel.
· Name the textbox as txtCutomerId.
Create a property CustomerID and bind it to Textbox Text property. Code given below:
To prevent blank entry in CustomerID textbox, add the following code on Form Closing event of Search form:
To add a dataset
· Got to Project -> Add New Item.
· Select Data from the Installed Templates.
· Select DataSet -> Enter CustomerDS.xsd in Name textbox.
Add a New DataTable (Customer) in the dataset as shown in the below screenshot and set the CustomerID as primary key and its data type to System.Int32. Change the data type of column DateofBirth to DateTime.
Add the following methods in your Customer form:
This method (GetNewCustomerID)will always return a new CustomerID:
The method (SetCustomer) set the customer data into the form fields. For this we have to pass the customer row as a parameter.
This method (ResetCustomer) reset the form fields after saving the data.
Create a new object of Customer DataTable at the class level:
On form load read the xml file (Customer.xml) if exists and set the new customer id by using GetNewCustomerID method.
On the click of save button add the following code. It will add the customer if the CustomerID not exists otherwise update the data of customer on the basis of CustomerID.
To delete the customer record, add the following on the click of Delete button.
To search a customer, paste the given code on the click event of Search button:
To find the first record from the DataTable:
To find next record from the DataTable:
To find previous record from the DataTable:
To find last record from the DataTable:
Now run or debug this application to see the output. You need to add some records in order to test it properly:
After adding some records you can also search records on the basis of CustomerID by using Search form.
The data will be saving in the xml file (Customer.xml) in given format:
Thanks for reading this article. You can enter your valuable
comments and suggestion to improve this article in the comment box.
In order to get the source code of this application, you can also enter your email id in the comment box.