Thursday, March 27, 2008

Databas to XML

---------------------
Test.aspx page
---------------------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>




Vinit XML




border="1">







Convert DataBase to XMl File

cellpadding="0" width="100%" align="center" border="0">















cellspacing="0" cellpadding="0" width="70%" border="1">





Select Table 


-- All --
Category
SubCategory
Product
Customer
Project
City
State
Country



Height="24px" runat="server" Text="Convert" Font-Bold="True" OnClick="Button1_Click1">









-----------------------------
Test.aspx.cs
-----------------------------
using System;
using System.Data;
using System.Configuration;
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 _Default : System.Web.UI.Page
{
//protected SqlConnection connect = new SqlConnection();


protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click1(object sender, EventArgs e)
{

SqlConnection connect = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
SqlCommand cmdXML;
SqlDataAdapter DScmdXML ;
DataSet DSXML = new DataSet();
string sqlXML;
string sqlXML1;
int i;
int j;
string strPCode = "";
string [] arrpcode;
System.IO.StreamWriter xmlSW;
for (i = 0; i <= ListBox1.Items.Count - 1; i++)
{
if (ListBox1.Items[i].Selected) {
if (strPCode == "")
{
strPCode = ListBox1.Items[i].Text;
}
else {
strPCode = strPCode + "," + ListBox1.Items[i].Text;
}
}
}

arrpcode = strPCode.ToString().Split('\'');
for (j = 0; j <= arrpcode.Length - 1; j++) {
try {
switch ((arrpcode[j]))
{
case "Product":

//Query for selecting Albumname and Imageid using 2 tables according to albums

//sqlXML1 ="select b.AlbumName ,c.ImageID from"+
// " tblImagemaster a , tblAlbumMaster b , tblAlbumImageDetails c " +"where"
// +" a.Imageid = c.Imageid and c.AlbumId = b.AlbumId";

//Query for selecting Albumname,Imageid and Imagename using 3 tables according to albums

sqlXML1 = "select b.AlbumName ,c.ImageID ,a.Imagename from" +
" tblImagemaster a , tblAlbumMaster b , tblAlbumImageDetails c " + "where"
+ "a.Imageid = c.Imageid and c.AlbumId = b.AlbumId";

//Other test with the query check XML accordingly generated

//sqlXML1 = "select b.AlbumName ,c.ImageID from" +
// " tblAlbumMaster b , tblAlbumImageDetails c " + "where"
// + " c.AlbumId = b.AlbumId";

sqlXML = sqlXML1;
cmdXML = new SqlCommand(sqlXML, connect);
DScmdXML = new SqlDataAdapter(cmdXML);
DScmdXML.Fill(DSXML, "AlbumName");
//DataRelation dr = new DataRelation("newrelation", DSXML.Tables[0].Columns[0], DSXML.Tables[1].Columns[1]);
//dr.Nested = true;
//DSXML.Relations.Add(dr);
Response.Write("" + "Successfully Converted Table " + arrpcode[j] + " to Imagerelationnew.xml" + "");
Response.Flush();
DSXML.WriteXml(Server.MapPath("Imagerelationnew.xml"), XmlWriteMode.WriteSchema);
System.IO.StreamWriter xmlSW2 = new System.IO.StreamWriter(Server.MapPath("Imagerelationnew.xml"));
DSXML.WriteXml(xmlSW2, XmlWriteMode.WriteSchema);
xmlSW2.Flush();
xmlSW2.Close();
break;
}
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
}



}

No comments: