-->

22 February 2020

Write xml file Using the WriteXml() Method ADO.NET

  Asp.Net CS By Example       22 February 2020
 Write xml file Using the WriteXml() Method ADO.NET 

 In this post we exploring about Dataset class.We can write the contents of the DataTable objects contained in a DataSet out to an XML file using the WriteXml() method. The XML file written by this method contains the DataTable name, the column names, and the column values.

 We can write the schema of a DataSet object out to an XML file using the WriteXmlSchema() method. The XML file written by this method contains the structure of the DataTable objects contained in the DataSet. Similarly, we can read the contents of the DataTable objects in an XML file into a DataSet object using the ReadXml() method. we can also read the schema contained in an XML file using the ReadXmlSchema() method.

 myDataSet.WriteXml("myXmlFile.xml"); 

 Below illustrates how to Write XML files using ADO.NET.



  Code: FrmDataSetWriteXmlFile.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OracleClient;
using System.Data;
using System.IO;
using System.Configuration;

namespace LearnAsp.Net.ControlDemo.ADO
{
    public partial class FrmDataSetWriteXmlFile : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            String query = " select * from emp ";
            OracleConnection objConn;
            // Retrieve Details from the DB
            string strConn = ConfigurationManager.ConnectionStrings["LearnAsp"].ToString();
            objConn = new OracleConnection(strConn);

            objConn.Open();
            //set command type as stored procedure
            OracleCommand objCmd = new OracleCommand();
            objCmd.Connection = objConn;
            objCmd.CommandText = query;
            objCmd.CommandType = CommandType.Text;
            DataTable dt = new DataTable();
            OracleDataAdapter oda = new OracleDataAdapter(objCmd);
            oda.Fill(dt);
            objConn.Close();
            dt.TableName = "employee";
            String FilePath = Server.MapPath("~\\GarbageBin\\myXmlFile.xml");
            dt.WriteXml(FilePath);

            objConn.Close();
        }
    }
}

  Output:

  Code: myXmlFile.xml
<DocumentElement>
  <employee>
    <EMPNO>7369</EMPNO>
    <ENAME>SMITH</ENAME>
    <JOB>CLERK</JOB>
    <MGR>7902</MGR>
    <HIREDATE>1980-12-17T00:00:00+05:30</HIREDATE>
    <SAL>800</SAL>
    <DEPTNO>20</DEPTNO>
  </employee>
  <employee>
    <EMPNO>7499</EMPNO>
    <ENAME>ALLEN</ENAME>
    <JOB>SALESMAN</JOB>
    <MGR>7698</MGR>
    <HIREDATE>1981-02-20T00:00:00+05:30</HIREDATE>
    <SAL>1600</SAL>
    <COMM>300</COMM>
    <DEPTNO>30</DEPTNO>
  </employee> 
  </employee>
</DocumentElement>

logoblog

Thanks for reading Write xml file Using the WriteXml() Method ADO.NET

Previous
« Prev Post

No comments:

Post a Comment

Please do not enter any spam link in the comment box.