XML as Middle Tier

Introduction

Hi,

There is always been a need that there will be an intelligent middle Layer between Database Server and the UI Code. There are many ways but one fantastic approach came in picture with the XML. There are rich classes in System.XML which make it pretty simple and effective. Here i had made a similar application where I am solving two purpose. One thing is still the things has not come as Code  Behind Approach in case of Mobile Forms and whatever till now we had done is not recognized under that approach.

And secondly using XML as a middle tier to DataBase Server and UI Code.

Check out this Application

Source Code

This Piece of code will generate the XML File and saved it in the given location and return the location in the form of String.

//WriteXML.cs
using System;
using System.Data;
using System.Xml;
using System.Data.SqlClient;
using System.IO;
using System.Text;
namespace WriteXML
{
public class WriteXML
{
public string genXMl()
{
String s="E:/ASPXSITE/DATAXML/myXmlData.xml";
SqlConnection myConn =
new SqlConnection
("server=localhost;uid=sa;pwd=;database=Shivani");
String mySql = "SELECT top 10 Name,Id from User_Detail";
SqlDataAdapter adapter =
new SqlDataAdapter();
adapter.SelectCommand =
new SqlCommand(mySql,myConn);
// Build the DataSet
DataSet myDs = new DataSet();
adapter.Fill(myDs, "User_Detail");
// Get a FileStream object
FileStream myFs = new FileStream(s,FileMode.OpenOrCreate,FileAccess.Write);
// Apply the WriteXml method to write an XML document
myDs.WriteXml(myFs);
myFs.Close();
return s;

}

compile this as

csc /r:System.dll,System.Data.dll,System.Xml.dll /target:library /out:GenXML.dll WriteXML.cs

//and save it in directory bin in u r server

Now here is our Code which will call this Class

<%@ Page Inherits="System.Web.UI.MobileControls.MobilePage" Language="C#" %>
<%@ Register TagPrefix="mobile" Namespace="System.Web.UI.MobileControls"
Assembly="System.Web.Mobile" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="WriteXML" %>
<script language="c#" runat="server" ID="Script1">
//initialize the variables
ArrayList nameinfo = new ArrayList();
public void SubmitBtn_Click(Object sender, EventArgs e)
{
//Everything been done here
WriteXML xml1 = new WriteXML();
string s = xml1.genXMl();
XmlDocument _doc =
new XmlDocument( );
//Provide the handle to the XML file
_doc.Load(s);
XmlNodeList _snames = _doc.GetElementsByTagName("Name");
XmlNodeList _sid = _doc.GetElementsByTagName("id");
//Take all the values and put them in a stringarray
for ( int _i = 0; _i < _snames.Count; ++_i )
{
string str = _snames[ _i].InnerText +" "+ _sid[ _i].InnerText;
nameinfo.Add(str);
}
// Bind the Array to the Mobile List
List1.DataSource = nameinfo;
List1.DataBind();
// Activate Second Form
ActiveForm = SecondForm;
}
</script>
<mobile:Form runat="server" id="FirstForm" BackColor="#336699" ForeColor="#ffffff">
<
mobile:Label runat="server" id="hida" Text= "Wanna check u r Diary" Font-Size="Large" />
<mobile:Command runat="server" OnClick="SubmitBtn_Click" Text="Go!" ID="Command1"/>
</
mobile:Form>
<
mobile:Form runat="server" id="SecondForm" Font-Name="Arial">
<
mobile:List id="List1" runat=server />
</mobile:Form>

This is an interesting approach as here even we can pass the query from the UI Page and even take the paramater or the input you had taken from the user. Let`s Check it out Next Time.


Similar Articles