xml serialization in .net

XML Serialization

      Serialization is the process of taking the state of an object and persisting it in some fashion. The Microsoft .NET Framework includes powerful objects that can serialize any object to XML. The System.Xml.Serialization namespace provides this capability.

Follow these steps to create a console application that creates an object, and then serializes its state to XML:
  1. In Visual C#, create a new Console Application project.
  2. On the Project menu, click Add Class to add a new class to the project.
  3. In the Add New Item dialog box, change the name of the class to clsPerson.
  4. Click Add. A new class is created.

    Note In Visual Studio .NET , click Open.
  5. Add the following code after the Public Class clsPerson statement
               public   string FirstName;
                 public   string MiddleName;
                 public   string LastName;
 
  1. Switch to the code window for Program.cs in Visual Studio or for Class1.cs in Visual Studio .NET
  2. In the void Main method, declare and create an instance of the clsPerson class:
        clsPerson p = new clsPerson();

     Set the properties of the clsPerson object:
           p.FirstName = "Naresh";
           p.MiddleName = "Kumar";
           p.LastName = "Kamuni";
       
  1. The Xml.Serialization namespace contains an XmlSerializer class that serializes an object to XML. When you create an instance of XmlSerializer, you pass the type of the class that you want to serialize into its constructor:
System.Xml.Serialization.XmlSerializer x =
                new System.Xml.Serialization.XmlSerializer(typeof(clsPerson));

  1. The Serialize method is used to serialize an object to XML. Serialize is overloaded and can send output to a TextWriter, Stream, or XMLWriter object. In this example,
  2. you send the output to the console:
                      x.Serialize(Console.Out,p);
       Console.WriteLine();
                     Console.ReadLine();

To verify that your project works, press CTRL+F5 to run the project.
A clsPerson object is created and populated with the values that you entered.
This state is serialized to XML. The console window shows the following:

<?xml version="1.0" encoding="IBM437"?>
 <clsPerson xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<FirstName>Naresh</FirstName>
 <MiddleName>Kumar</MiddleName>
<LastName>Kamuni</LastName>
</clsPerson>
(OR) 

 you send the output to the Stream:
System.IO.FileStream stream =
                    new System.IO.FileStream("XMLFile1.xml", FileMode.Create);
 x.Serialize(stream, p);

then your XMLFile.xml file is created in your Debug folder

The following snopshot is shows code about send the output to Stream:


  1. 
    

Explain the page life cycle in ASP.NET :



ASP.NET  Page Life Cycle - The lifetime of an ASP.NET Page is filled with events. A series of processing steps takes place during this page life cycle. Following tasks are performed:

* Initialization
* Instantiation of controls
* Restoration & Maintainance of State
* Running Event Handlers
* Rendering of data to the browser

The life cycle may be broken down into Stages and Events. The stages reflect the broad spectrum of tasks performed. The following stages take place

Page life cycle Stages:

1) Page request - This is the first stage, before the page life cycle starts. Whenever a page is requested, ASP.NET detects whether the page is to be requested, parsed and compiled or whether the page can be cached from the system.
2) Start - In this stage, properties such as Request and Response are set. Its also determined at this stage whether the request is a new request or old, and thus it sets the IsPostBack property in the Start stage of the page life cycle.
3) Page Initialization - Each control of the page is assigned a unique identification ID. If there are themes, they are applied. Note that during the Page Initialization stage, neither postback data is loaded, nor any viewstate data is retrieved.
4) Load - If current request is a postback, then control values are retrieved from their viewstate.
5) Validation - The validate method of the validation controls is invoked. This sets the IsValid property of the validation control.
6) PostBack Event Handling –Event handlers are invoked, in case the request is a postback.
7) Rendering - Viewstate for the page is saved. Then render method for each control is called. A textwriter writes the output of the rendering stage to the output stream of the page's Response property.
8) Unload - This is the last stage in the page life cycle stages. It is invoked when the page is completely rendered. Page properties like Respone and Request are unloaded.

Note that each stage has its own events within it. These events may be used by developers to handle their code. Listed below are page events that are used more frequently.

Page life cycle Events:

PreInit - Checks the IsPostBack property. To create or recreate dynamic controls. To set master pages dynamically. Gets and Sets profile propety values.
Init - Raised after all controls are initialized, and skin properties are set.
InitComplete - This event may be used, when we need to be sure that all initialization tasks are complete.
PreLoad - If processing on a control or a page is required before the Load event.
Load - invokes the OnLoad event on the page. The same is done for each child control on the page. May set properties of controls, create database connections.
Control Events - These are the control specific events, such as button clicks, listbox item selects etc.
LoadComplete - To execute tasks that require that the complete page has been loaded.
PreRender - Some methods are called before the PreRenderEvent takes place, like EnsureChildControls, data bound controls that have a dataSourceId set also call the DataBind method.
Each control of the page has a PreRender event. Developers may use the prerender event to make final changes to the controls before it is rendered to the page.
SaveStateComplete - ViewState is saved before this event occurs. However, if any changes to the viewstate of a control is made, then this is the event to be used. It cannot be used to make changes to other properties of a control.
Render - This is a stage, not an event. The page object invokes this stage on each control of the page. This actually means that the ASP.NET server control's HTML markup is sent to the browser.
Unload - This event occurs for each control. It takes care of cleanup activities like wiping the database connectivities.


what is the difference between session.abandon() and session.clear() ?

 Session.Abandon():

          Session.Abandon() destroys the session
       Abandon will destroy the session completely, meaning that you need to begin a new session before you can store any more values in the session for that user.
 Session.Clear():

Session.Clear() just removes all values

                Clearing the session will not unset the session, it still exists with the same ID for the user but with the values simply cleared.
The Clear method removes all keys and values from the current session. Compare to Abandon method, it doesn’t create the new session, It just make all variables in session to NULL.


Diff B/w Session.Clear() and Session.RemoveAll():

actually there is no difference. Session.RemoveAll() methods internally makes a call to Clear() method only.


duplex contract in WCF


What are different bindings supported by WCF?

a. BasicHttpBinding
b. WSHttpBinding
c. WSDualHttpBinding
d. WSFederationHttpBinding
e. NetTcpBinding
f. NetNamedPipeBinding
g. NetMsmqBinding
h. NetPeerTcpBinding
i. MsmqIntegrationBinding

What is duplex contract in WCF?

Duplex contract: It enables clients and servers to communicate with each other. The calls can be initiated independently of the other one. The duplex contract is one the message patterns available to Windows Communication Foundation (WCF) services. It comprises of two one-way contracts.

Different transaction isolation levels in WCF

  • Read Uncommitted: - Also known as Dirty isolation level. It makes sure that corrupt Data cannot be read. This is the lowest isolation level
  • Read Committed: - It ensures not to read the data that has been changed by any other application and is not yet committed. It is the default level.
  • Repeatable Read: - It stops the usage of dirt read and non-repeatable read. It states that data fetched through a query will be locked and will not be updated by any other transaction.
  • Serializable: - It does not allow any modification and addition of new data till the transaction is completed. This is considered to be a very restrictive level.
  • Snapshot: - It raises error on modifying a data that has already been changed by any transaction.

components of WCF

WCF is a framework that builds applications that can inter-communicate based on service oriented architecture consuming secure and reliable web services. This also helps in Distributed computing. The WCF also brings together the various communication models into a single model.


WCF Service is composed of three components:
  • Service class: It implements the service needed.
  • Host environment: is an environment that hosts the developed service.
  • Endpoints: are the connection points for the clients to connect to the service. Clients find the end points through three components like service contract, binding, and address. 



Difference between WCF and Web Services

WCF can create services similar in concept to ASMX, but has much more capabilities. WCF is much more efficient than ASP.Net coz it is implemented on pipeline. WCF is more secure, reliable. As WCF is implemented on a different pipeline it does not have all Http capabilities (Http session, cache etc).