So first blog under “What I Learned Today” and don’t be surprised it’s pure technical.
There is a need in my project to generate XML file using Typed dataset and XSD. XSD is nothing but XML Schema Definition.
For adding dataset in C# project, right click on project click on Add New Item and select dataset.
For adding columns and tables in the XSD file, open it in designer view right click on the file and select the appropriate context menu. Second option available is you can simply Drag & Drop database table on XSD.
Once you are ready with the XSD, code for dataset manipulation goes as follows:
EmployeeDS ds = new EmployeeDS();
DataTable table = ds.Tables["EmployeeDS"];
///programmatically adding tables and columns
/*DataTable table = ds.Tables.Add(“Employee”);
table.Columns.Add(“Employee Name”, Type.GetType(“System.String”));
table.Columns.Add(“Employee Description”, Type.GetType(“System.String”));
table.Columns.Add(“Salary”, Type.GetType(“System.String”));
table.Columns.Add(“Phone”, Type.GetType(“System.String”));
table.Columns.Add(“Address”, Type.GetType(“System.String”));*/
/// Another way of accessing user control
//(theUserControl as usrcntrlNewEmployee)
DataRow row;
row = table.NewRow();
row["EmployeeName"] = ((usrcntrlNewEmployee)theUserControl).EmployeeNAME;
row["EmployeeDescription"] = ((usrcntrlNewEmployee)theUserControl).EmployeeDESCRIPTION;
row["Salary"] = ((usrcntrlNewEmployee)theUserControl).Salary;
row["Phone"] = ((usrcntrlNewEmployee)theUserControl).Phone;
row["Address"] = ((usrcntrlNewEmployee)theUserControl).Address;
table.Rows.Add(row);
/// Writting string to buffer
//Dim sw As New System.IO.StringWriter
//ds.WriteXml(sw);
ds.WriteXml(“C:\\Prashant\\Employee.xml”);
As simple as that
Reference:
Typed Dataset & XSD
