Search This Blog

Tuesday, December 28, 2010

Serialization and De-Serialization Example

Create a webapplication(asp.net application ) using visual studio. Now create the below class
[Serializable]
    public class patient
    {
        private static patient p;
        private int _age;
        public int Age
        {
            get
            {
                return _age;
            }
            set
            {
                _age = value;
            }
        }
        private patient()
        {

        }
        public static patient getPatientObject()
        {
            if (p == null)
            {
                p = new patient(); return p;
            }
            else
            {
                return p;
            }
        }
        public static void SerializeToXML()
        {
            //Note: XmlSerializer is a Protected class
            patient pat = new patient();
            XmlSerializer serializer = new XmlSerializer(typeof(patient));
            TextWriter textWriter = new StreamWriter(@"C:\serializedpatient.xml");
            serializer.Serialize(textWriter, pat);
            textWriter.Close();
        }
        public static patient DeserializeFromXml()
        {
            TextReader tr=new StreamReader(@"c:\serializedpatient.xml");
            XmlSerializer serializer = new XmlSerializer(typeof(patient));
            patient p=(patient)serializer.Deserialize(tr);
            return p;
        }
    }
Next Steps: 1. Now Create a .aspx page with name SerializationSample.aspx
2.Create 2 Buttons with name Serialize( button id=btnSerialize) De-Serialize ( button id=btnDeserialize)
3.Create handlers for these two buttons. Call the methods "patient.SerializeToXML(); " and "patient.DeserializeFromXml();" from the clickeventhandler methods btnSerialize_click,btnDeserialize_click respectively.
For Best Android Training Click Here:Android training in Bangalore

Thursday, December 23, 2010

what is Serialization and when to do it?

Student: What is Serialization?
TechPalle: Serialization is the process of converting an object into a stream. stream can be xml stream or it can be a byte stream.
Student: Why should i do Serialization?
TechPalle: Serialization is required many times in Real time programming.
Below are the examples of when to use Serialization.
Ex 1: In the 3-tier architecture let us say we have BAL,DAL and UI layer. Now assume these projects deployed in three different machines. now i want to get an object of some class from one tier to other tier or application in another computer. So if you want to send objects from one computer to another , you cannot transfer the object in its original form . But you need to convert the object into some form of stream and then send the stream to another computer. Now after receiving the stream then get the object back by DE-serializing the object back from the stream. This is the common situation where you have to do Serialization and De-Serialization. See complete example of Serialization and De serialization in the next article.

Friday, December 10, 2010

Difference between XML and HTML

1.HTML is a presentation language ( What ever elements you create by using HTML user can see thoas as GUI elements in the browser)
2.XML is used especially for storing Data and Transporting the Data
3.Apart from the above fundamental difference there are syntactical differences also.
4.Xml is a strongly typed language and HTML is loosely typed language
Ex: If you miss any ending tag in the XML your document will not be parsed and XML Parser gives you an Error. In case of HTML if you miss any ending tag then also your HTML code will be executed.
5.XML is case sensitive Ex: <Name>and <name>are not same
6.HTML is not case sensitive Ex: <Title>and<title> are same

What is XML

1.XML stands for Extensible mark-up language
2.XML is used for data storage.
3.XML is an easy transporation mechanism , in case of corporate environments usually fire walls will stop the data before enetering into corporate network but xml files can be easily transporated even in presence of fire-walls.
4.XML is a strongly types language ( Means the XML Parser checks the syntax thoroughly if any descripency found the parser through an exception and out put will not be given).
5.But incase of HTML if you have any errors, HTML Parser will still execute your HTML Code.
6.XML is mainly used for Transporatation and Storage.
Palle Technologies providing Best .Net Training http://techpalle.com/net-training-in-bangalore.html

What is XML Parser

1.All xml files or documents will be converted into xml DOM Object by XML Parser.
2.All new browsers will be having XML Parsers.
3.XML DOM is a graphical representation of the nodes in browsers memory.
Ex: Let us say i have below "Students.xml" file .
<Students>
<Student> <Name>Sreepriya</Name> </Student>
<Student><Name>Subhashini</Name></Student>
</Students>
Explanation: Now Parser will read the above xml file and prepare Logical graphs for each and every node just like a tree.Please see the below diagram for how the DOM tree looks like.

XML Assignments

1.What is xml, how the xml will be parsed, who do parsing
2.what is element,node,attribute in xml.
3.Validating the xml file using DTD
4.Sax Parser and DOM Parser
5.What is xml Namespace,CDATA,PCDATA
6.What is XML Island
7.What is XML DOM
8.How to read xml file by using C#
9.How to modify data in xml by using C#
10.What is Xpath
11.What is Xquery

Resources needs to be used is
http://www.w3schools.com/
MSDN Library or Google is the best resource.

Sunday, November 28, 2010

Output caching by using VaryByParam attribute

you can able to cache multiple versions of the same page by varying the query string value.
Example:
1.Create a page called City.aspx with the below code.
2.Replace your html body code with the below code
<form id="form1" runat="server">
<div>
<asp:label id="lblCity" text="Select a city:" runat="server">
<asp:listbox id="lbCity" runat="server" autopostback="True" onselectedindexchanged="lbCity_SelectedIndexChanged">
<asp:listitem text="Bangalore" value="1">
<asp:listitem text="Hyderabad" value="2">
<asp:listitem text="Chennai" value="3">
</asp:listitem></asp:listitem></asp:listitem></asp:listbox>
</asp:label></div>
</form>3.Create selected index changed event for List Box with the name lbCity_SelectedIndexChanged in the City.aspx.cs.
protected void lbCity_SelectedIndexChanged(object sender, EventArgs e)
{
Response.Redirect("VaryByParam.aspx?city=" + lbCity.SelectedItem.Text);
}

4.Create an another page called VaryByParamTest.aspx
5.In the html code of the VaryByParamTest.aspx page copy paste the below line of code immediately after the "@ Page" directive line of code

6.In the page load of the VaryByParamTest.aspx.cs write below code.
protected void Page_Load(object sender, EventArgs e)
{
Response.Write(DateTime.Now.ToString());
}
Testing the Code:
1.Click on F5 by setting the City.aspx as the start page.
2.Select any item in the list box ( for example You select Bangalore )
3.Observe the server time in the browser
4.Open another browser and access the city.aspx again by opening the application (ex: http://localhost:1034/YourApplicationName/City.aspx)
5.Now you select the Hyderabad then observe the changed time ( This means another version of the page is displaying)
6.Again you open another browser and copy paste the below URL (http://localhost:1034/YourApplicationName/City.aspx)
7. now observer the server time ( you will find the server time same as first browser). This means already one page is cached for Bangalore and now another page cached for Hyderabad.
8.Now you open another browser,access application and select Hyderabad.You will find the server time same as second browser time.
9.If you have any issues you can always contact .Net Training Bangalore
Note: Palle Technologies is the only institute who can provide excellent computer education at low cost and high quality. Get an assured placements by joining Palle Technologies.