.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month

Home >> Code Snippets >> File Operation >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Display table contents into an XML file using ASP.NET

Posted By:Athira Sreejith       Posted Date: March 24, 2009    Points: 10    Category: File Operation    URL: http://www.dotnetspark.com  

Get the table content in XML file from SQL server pragmatically using below code.This code will import the Data from SQl Server to .xml file. For this we have use SQL Query to fetch data from SQL server and assign this to DataSet and from DataSet we export the data into .XML file.

Lets see and example in C#

using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using System.Data.SqlClient;

public partial class converttbltoxml : System.Web.UI.Page


protected void Page_Load(object sender, EventArgs e)



protected void btnsubmit_Click(object sender, EventArgs e)


SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]);


string qry = "select * from " + lstfill.SelectedItem.Text + "";

SqlCommand cmd = new SqlCommand(qry, con);

SqlDataAdapter ad = new SqlDataAdapter(cmd);

DataSet ds=new DataSet();

ad.Fill(ds, lstfill.SelectedItem.Text);


string tblname=lstfill.SelectedItem.Text+".xml";

ds.WriteXml(""+tblname+"", XmlWriteMode.WriteSchema);

System.IO.StreamWriter xmlwriter = new System.IO.StreamWriter(""+tblname+"");

ds.WriteXml(xmlwriter, XmlWriteMode.WriteSchema);





In Vb.NET code Example

Imports System

Imports System.Data

Imports System.Configuration

Imports System.Collections

Imports System.Web

Imports System.Web.Security

Imports System.Web.UI

Imports System.Web.UI.WebControls

Imports System.Web.UI.WebControls.WebParts

Imports System.Web.UI.HtmlControls

Imports System.Data.SqlClient

Public Partial Class converttbltoxml
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    End Sub
    Protected Sub btnsubmit_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim con As New SqlConnection(ConfigurationManager.AppSettings("ConnectionString"))
        Dim qry As String = "select * from " & lstfill.SelectedItem.Text & ""
        Dim cmd As New SqlCommand(qry, con)
        Dim ad As New SqlDataAdapter(cmd)
        Dim ds As New DataSet()
        ad.Fill(ds, lstfill.SelectedItem.Text)
        Dim tblname As String = lstfill.SelectedItem.Text & ".xml"
        ds.WriteXml("" & tblname & "", XmlWriteMode.WriteSchema)
        Dim xmlwriter As New System.IO.StreamWriter("" & tblname & "")
        ds.WriteXml(xmlwriter, XmlWriteMode.WriteSchema)
    End Sub
End Class


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend