I want it should generate xml code and give xml download link so user can download xml file in their machine and than use it.
Check this example. Now please take its reference and correct your code.
HTML
<asp:Button ID="Button1" runat="server" OnClick="GenerateXML" Text="Generate XML" />
<asp:Button ID="Button2" runat="server" OnClick="DownloadXML" Text="Download XML" />
Namespaces
C#
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
VB.Net
Imports System.Data.SqlClient
Imports System.Data
Code
C#
protected void GenerateXML(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings[1].ConnectionString);
SqlDataAdapter dt = new SqlDataAdapter("SELECT * FROM Customers", con);
DataSet ds = new DataSet();
dt.Fill(ds, "Customers");
ds.WriteXml(Server.MapPath("~/Customers.xml"));
}
protected void DownloadXML(object sender, EventArgs e)
{
if (System.IO.File.Exists(Server.MapPath("~/Customers.xml")))
{
DownloadFile(Server.MapPath("~/Customers.xml"));
}
}
private void DownloadFile(string filePath)
{
Response.ContentType = "application/xml";
Response.AddHeader("Content-Disposition", "attachment;filename=Customers.xml");
Response.WriteFile(filePath);
Response.End();
}
VB.Net
Protected Sub GenerateXML(ByVal sender As Object, ByVal e As EventArgs)
Dim con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings(1).ConnectionString)
Dim dt As SqlDataAdapter = New SqlDataAdapter("SELECT * FROM Customers", con)
Dim ds As DataSet = New DataSet()
dt.Fill(ds, "Customers")
ds.WriteXml(Server.MapPath("~/Customers.xml"))
End Sub
Protected Sub DownloadXML(ByVal sender As Object, ByVal e As EventArgs)
If System.IO.File.Exists(Server.MapPath("~/Customers.xml")) Then
DownloadFile(Server.MapPath("~/Customer.xml"))
End If
End Sub
Private Sub DownloadFile(ByVal filePath As String)
Response.ContentType = "application/xml"
Response.AddHeader("Content-Disposition", "attachment;filename=Customers.xml")
Response.WriteFile(filePath)
Response.[End]()
End Sub
Generated xml
<?xml version="1.0" standalone="yes"?>
<NewDataSet>
<Customers>
<CustomerId>1</CustomerId>
<Name>Mudassar Khan</Name>
<Country>India</Country>
</Customers>
<Customers>
<CustomerId>2</CustomerId>
<Name>Maria </Name>
<Country>Austria</Country>
</Customers>
<Customers>
<CustomerId>3</CustomerId>
<Name>Ana Trujillo </Name>
<Country>France</Country>
</Customers>
<Customers>
<CustomerId>4</CustomerId>
<Name>Antonio Moreno </Name>
<Country>Brazil</Country>
</Customers>
<Customers>
<CustomerId>5</CustomerId>
<Name>Christina Berglund</Name>
<Country>Ireland</Country>
</Customers>
</NewDataSet>