Site sponsored by: Idera Try Idera’s new SQL admin toolset
SQL Server Performance

  • Home
  • Articles
  • Forums
  • Tips
  • Quiz
  • FAQ's
  • Blogs
  • Software
  • Books
  • About Us
RSS Feeds
Sign in | Join


Article Topics

All Articles
Performance Tuning
Audit
Business Intelligence
Clustering
Reporting Services
Developer
General DBA
ASP.NET / ADO.NET

Write for Us

Share you SQL Server knowledge with others and raise your profile in the community More...
Latest Articles

Server and Database Auditing in SQL Server 2008
So, you find yourself On-Call
Administrator & Monitoring Change Data Capture in SQL Server 2008 ...
Importance of the Resource Database

More     
 
Latest FAQ's

SQL Server Reporting Server (SSRS) service is failing to start ...
Cannot Start SQL Server Service
Users are able to connect to report manager but not able ...
Errors when SQL Server Snapshot Replication is Running

More     
   
Latest Software Reviews

Spotlight on ApexSQL Doc 2008
ApexSQL Enforce
Embarcadero Change Manager
SQL Server DBA Dashboard

More     

articles >> asp.net / ado.net >> DataSets and XML – A Perfect Combination ...

DataSets and XML – A Perfect Combination

By : Joydip Kanjilal
Jan 22, 2007

Page 3 / 3

Update Data in a XML file using DataSet


The code that follows updates the xml document with the most recent data in the DataSet instance. It works the same way as the above method (DeleteData) in the sense that it accepts the employee ID representing the employee record to be updated. It searches the same and updates the employee record if one exists.

private void UpdateData(int empID)
{
  string filePath = …; //Some path indicating where the xml
//file is stored
  DataSet dataSetEmployee = GetEmployeeDataSet();
  DataTable dataTableEmployee =  dataSetEmployee.Products.Tables[0];
  DataView dataViewEmployee = new DataView();
  dataViewEmployee = dataTableEmployee.DefaultView;
  dataViewEmployee.Sort = "Emp_ID";
  int rowIndex =dvProducts.Find("empID");
  if (rowIndex ==  - 1)
    Response.Write("This employee code does not exist");
  else
  {
    dataViewEmployee.RowFilter = " Emp_ID =" + empID;
    dataViewEmployee[0][“Emp_Name”] = “Joydip”;
    dataSetEmployee.WriteXml(filePath,XmlWriteMode.WriteSchema);
  }
}

Delete Data from XML File using a DataSet

The following code illustrates how we can delete data from an XML file using a DataSet. The method DeleteData accepts the employee ID as the parameter searches the employee record in the DataSet instance and deletes the record if one exists.

private void DeleteData(int empID)
{
  string filePath = …; //Some path indicating where the xml file is stored
  DataSet dataSetEmployee = GetEmployeeDataSet();
  DataTable dataTableEmployee = dataSetEmployee.Products.Tables[0];
  DataView dataViewEmployee = new DataView();
  dataViewEmployee = dataTableEmployee.DefaultView;
  dataViewEmployee.Sort = "Emp_ID";
  int rowIndex = dataViewEmployee.Find("empID");
  if (rowIndex ==  - 1)
    Response.Write("This employee code does not exist");
  else
  {
    dataViewEmployee.RowFilter = " Emp_ID =" + empID;
    dataViewEmployee.Delete(0);
    dataSetEmployee.WriteXml(filePath,XmlWriteMode.WriteSchema);
  }
}

Note that in both of these above methods (UpdateData & DeleteData) you can alternately call the SearchData method to find whether the employee record exists prior to updating or deleting the record. This would minimize a lot of code redundancy.

References

Please refer to the following links for further reference on this topic:--
DataGrids, DataSets, and XML Strings
Using XML in a DataSet
DataSets and Xml Representation
Smarten Up Your DataSets with XML Schema


Points to be noted

Note that if the XML is generated originally from a DataSet, using DataSet.WriteXml method, you can directly import it with DataSet.ReadXml method. On the other hand, if the XML data is in some other format, you need to read the Xml into an XmlDocument object and then write the necessary code to import the records one after the other, or use XSLT to transform the XML data into a format that can be understood by the DataSet class.
Note that when writing data to a XML file you can choose if you want to write the Schema only, Schema and Records, or just the records.  When you read the XML data you can also decide if the tables and columns should be created based on the schema, or a new schema should be created and then the data XML read, ignoring the fields that are not present in the dataset or the datatable instance.
The DataSet class is generic in the sense that it is independent of the source of the data that is populating it. This is why, unlike a DataReader, we have only one type of DataSet. Note that we have DataReaders for each type of Data Providers, i.e., each type of databases.
A point of advice: Avoid using DataSets if you only require using it as a data container, i.e., a container of data. I would recommend using a Custom Business Entity class instead that would boost the performance to a large extent due to avoidance of un-necessary boxing and un-boxing issues. Refer to my article at Passing Data through Layers that illustrates how we can build and implement a custom business entity class in lieu of a DataSet to pass data through the layers of an application.


Conclusion

This article has discussed how we can use DataSets and XML to perform the CRUD operations without having an underlying database like SQL Server, Oracle, etc. This approach is cost effective but can suit environments where the volume of data being handled in much less. This is not a recommended solution in environments where we require voluminous data handling as it would consume a lot of memory and hence become a nightmare. Further, the disk I/O would also be very high, hence degrading the performance of the application as a whole.  Reader’s queries and suggestions are welcome.

<< Prev Page         








Home | Peformance Articles | Audit Articles | Business Intelligence Articles | Clustering Articles | Developer Articles | Reporting Services Articles | DBA Articles | ASP.NET / ADO.NET Articles | DBA FAQ's | Developer Peformance FAQ's | DBA Peformance FAQ's | Developer FAQ's | Clustering FAQ's | Error Messages | Audit Tool Reviews | Backup Tool Reviews | Coding Tool Reviews | Compare Tool Reviews | Documentation Tool Reviews | Design Tool Reviews | Monitoring Tool Reviews | Log Tool Reviews | Reporting Tool Reviews | Clustering Tool Reviews | Security Tool Reviews | Change Management Tool Reviews | Remote Access Tool Reviews | Book Reviews | Security Tool Reviews | QDPMA Performance Tuning | ADO.NET / ASP.NET | Administration | Analysis/OLAP Services | Application Development | Configuration | Components | ETL | Hardware | High Availability | Hints | Index | Misc | Operating Systems | Performance Tuning | Replication | T-SQL | Views


              © 1999-2008 by T10 Media. All rights reserved