Sign In| Join
Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Apr 11, 2012 01:11 AM by Decker Dong - MSFT
Member
111 Points
178 Posts
Apr 09, 2012 02:12 AM|LINK
I want to generate xml file using sql statements in c#.
can I?
how?
because, I want xml file to be refresh anytime, it is not static, it must be dynamic in my case.
eg: I want to show some data on screen.
name,
address,
phone.
I want to show this data using Xml file.
data will be changed or updated anytime, when I click certain button.
I will use gridview or data control to display data,
then I will bind this control to xml file.
then I want to update xml file using sql statement in c#.
does anyone have idea, share me.
pls
Star
7625 Points
1454 Posts
Apr 09, 2012 04:17 AM|LINK
http://forums.asp.net/t/1338552.aspx/1
9767 Points
1931 Posts
Apr 09, 2012 04:59 AM|LINK
skko I want to generate xml file using sql statements in c#. can I? how?
Yes.. try this.
Select top 5 * from dbo.users for XML path
If you want to query the table you can go through my below blog..
http://kavstech.blogspot.com/2009/08/create-xml-after-querying-on-table.html
Apr 09, 2012 06:00 AM|LINK
thanks for all reply:
now I also found one article on this link, http://www.c-sharpcorner.com/Blogs/8115/generating-xml-file-from-sql-database-using-C-Sharp.aspx
now I am doing like below:
first I created empty test.xml file in my web application project.
then I create one page.
default.aspx
<asp:Button ID="Button1" runat="server" Text="GenerateXML" OnClick="Button1_Click" />
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="calledXmlDataSource"> <ItemTemplate>
<asp:Label ID="label3" Text="ID: " runat="server"></asp:Label><%#XPath("id")%><br /> <asp:Label ID="label1" Text="Customer: " runat="server"></asp:Label><Strong><%# XPath("customerName") %><br /></Strong> </ItemTemplate> </asp:Repeater>
<asp:XmlDataSource ID="calledXmlDataSource" runat="server" DataFile="~/test.xml"></asp:XmlDataSource>
in codebehind: default.aspx.cs
protected void Page_Load(object sender, EventArgs e) { calledXmlDataSource.DataBind(); Repeater1.DataBind(); }
protected void Button1_Click(object sender, EventArgs e) { int status=1; int areaID=2; int counterID=3; string sql2 = "Select id, customerName From Queue"; SqlConnection con = new SqlConnection("Data Source=(local);Initial Catalog=db;Integrated Security=True;"); SqlDataAdapter dt = new SqlDataAdapter(sql2, con); DataSet ds = new DataSet(); dt.Fill(ds, "Called"); ds.WriteXml(Server.MapPath("test.xml")); BindRepeater(); //bind to update repeater Label1.Text = "Xml generated"; Label1.Text += " <a href=test.xml>XML file</a>"; }
private void BindRepeater() { calledXmlDataSource.DataBind(); Repeater1.DataBind(); }
as you see in my code, I bind repeater control with xml file in pageLoad.
CustomerName & Address appear on page load.
customername changes any time, not static.
when I click "GenerateXML" button, I fetch new customerName, id from db and create xml file.
then showing on repeater control, using xmlDataSource.
so, I databind xmlDatasource and repeater control on GenerateXML button Click event.
but,
my repeater control not refresh, when I click button.
Only if page load agains, I can see data changes on repeater.
how to do?
11225 Points
1815 Posts
Apr 09, 2012 06:35 AM|LINK
refer these links
http://csharpdotnetfreak.blogspot.com/2012/06/read-xml-file-into-datatable-aspnet-cvb.html
http://csharpdotnetfreak.blogspot.com/2012/06/read-write-xml-file-c-vb-aspnet.html
http://csharpdotnetfreak.blogspot.com/2011/12/gridview-xml-edit-delete-insert-update.html
Apr 09, 2012 08:05 AM|LINK
thanks amit.jain,
according to your links, I changed gridview control instead of repeater control.
that's fine,
when I click button,
first, I generate xml file,
then, bind gridview with this xml file.
I put these two events into button click function.
thanks all.
All-Star
118619 Points
18779 Posts
Apr 11, 2012 01:11 AM|LINK
Hello skko:)
【Solutions】
An easy way to generate an xml file for you now is to use DataSet.Tables[0].WriteToXml,and then you should use SqlDataAdapter.Fill(DataSet) and then generate a memory-based dataTable and export it as an xml file。
skko
Member
111 Points
178 Posts
to generate xml file
Apr 09, 2012 02:12 AM|LINK
I want to generate xml file using sql statements in c#.
can I?
how?
because, I want xml file to be refresh anytime, it is not static, it must be dynamic in my case.
eg: I want to show some data on screen.
name,
address,
phone.
I want to show this data using Xml file.
data will be changed or updated anytime, when I click certain button.
I will use gridview or data control to display data,
then I will bind this control to xml file.
then I want to update xml file using sql statement in c#.
does anyone have idea, share me.
pls
santosh.jagd...
Star
7625 Points
1454 Posts
Re: to generate xml file
Apr 09, 2012 04:17 AM|LINK
http://forums.asp.net/t/1338552.aspx/1
MCP
kavita_khand...
Star
9767 Points
1931 Posts
Re: to generate xml file
Apr 09, 2012 04:59 AM|LINK
Yes.. try this.
Select top 5 * from dbo.users for XML path
If you want to query the table you can go through my below blog..
http://kavstech.blogspot.com/2009/08/create-xml-after-querying-on-table.html
I would love to change the world, but they wont give me the source code.
skko
Member
111 Points
178 Posts
Re: to generate xml file
Apr 09, 2012 06:00 AM|LINK
thanks for all reply:
now I also found one article on this link, http://www.c-sharpcorner.com/Blogs/8115/generating-xml-file-from-sql-database-using-C-Sharp.aspx
now I am doing like below:
first I created empty test.xml file in my web application project.
then I create one page.
default.aspx
<asp:Button ID="Button1" runat="server" Text="GenerateXML" OnClick="Button1_Click" />
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="calledXmlDataSource">
<ItemTemplate>
<asp:Label ID="label3" Text="ID: " runat="server"></asp:Label><%#XPath("id")%><br />
<asp:Label ID="label1" Text="Customer: " runat="server"></asp:Label><Strong><%# XPath("customerName") %><br /></Strong>
</ItemTemplate>
</asp:Repeater>
<asp:XmlDataSource ID="calledXmlDataSource" runat="server" DataFile="~/test.xml"></asp:XmlDataSource>
in codebehind: default.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
calledXmlDataSource.DataBind();
Repeater1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
int status=1;
int areaID=2;
int counterID=3;
string sql2 = "Select id, customerName From Queue";
SqlConnection con = new SqlConnection("Data Source=(local);Initial Catalog=db;Integrated Security=True;");
SqlDataAdapter dt = new SqlDataAdapter(sql2, con);
DataSet ds = new DataSet();
dt.Fill(ds, "Called");
ds.WriteXml(Server.MapPath("test.xml"));
BindRepeater(); //bind to update repeater
Label1.Text = "Xml generated";
Label1.Text += " <a href=test.xml>XML file</a>";
}
private void BindRepeater()
{
calledXmlDataSource.DataBind();
Repeater1.DataBind();
}
as you see in my code, I bind repeater control with xml file in pageLoad.
CustomerName & Address appear on page load.
customername changes any time, not static.
when I click "GenerateXML" button, I fetch new customerName, id from db and create xml file.
then showing on repeater control, using xmlDataSource.
so, I databind xmlDatasource and repeater control on GenerateXML button Click event.
but,
my repeater control not refresh, when I click button.
Only if page load agains, I can see data changes on repeater.
how to do?
amit.jain
Star
11225 Points
1815 Posts
Re: to generate xml file
Apr 09, 2012 06:35 AM|LINK
refer these links
http://csharpdotnetfreak.blogspot.com/2012/06/read-xml-file-into-datatable-aspnet-cvb.html
http://csharpdotnetfreak.blogspot.com/2012/06/read-write-xml-file-c-vb-aspnet.html
http://csharpdotnetfreak.blogspot.com/2011/12/gridview-xml-edit-delete-insert-update.html
amiT jaiN
ASP.NET C# VB Articles And Code Examples
skko
Member
111 Points
178 Posts
Re: to generate xml file
Apr 09, 2012 08:05 AM|LINK
thanks amit.jain,
according to your links, I changed gridview control instead of repeater control.
that's fine,
when I click button,
first, I generate xml file,
then, bind gridview with this xml file.
I put these two events into button click function.
thanks all.
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: to generate xml file
Apr 11, 2012 01:11 AM|LINK
Hello skko:)
【Solutions】
An easy way to generate an xml file for you now is to use DataSet.Tables[0].WriteToXml,and then you should use SqlDataAdapter.Fill(DataSet) and then generate a memory-based dataTable and export it as an xml file。