Wednesday, August 3, 2011

Debugging Asp.net application using XML Serialization

 

Debugging on Test or Production server is very difficult task for developers.First of all developers do not have any rights to touch the Test or Production environment and he received the list of bugs mainly due to data problem on production or Test Server.When developers try to reproduce the bugs on development environment is not possible because data development environment is accurate.So I have found out the solution of this problem through XML Serialization techniques which describe as below:-

In this article I am taking a example of simple Employee Detail.I will explain how we can debug using XML Serialize data.

Example

First I have declare a Employee class and create one method for creating employee details.

public class Employee
{
public string EmployeeName { get; set; }
public string Address { get; set; }
public int MobilNo { get; set; }


public List<Employee> GetEmployees()
{
List<Employee> Emplst = new List<Employee>();

for (int i = 0; i < 5; i++)
{
Employee emp = new Employee();
emp.EmployeeName = "Employee" + "-" + i.ToString();
emp.Address = "Address" + "-" + i.ToString();
emp.MobilNo = 9899 + (i + 2);
Emplst.Add(emp);
}
return Emplst;
}

}



 



Now create one page and call the GetEmployee() Method and fill the grid.



 public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Employee objEmp= new Employee();

GridView1.DataSource = objEmp.GetEmployees();
GridView1.DataBind();
}
}





Scenario 1



It is very simple code to populate grid from GetEmployees() method but you assume that data is coming from the database and populating the grid and at this time you are facing problem .Now you can track or debug the application with XML Serialization.



protected void Page_Load(object sender, EventArgs e)
{
Employee objEmp = new Employee();

SaveXMLData(objEmp.GetEmployees(), "Employee.XML");
GridView1.DataSource = ReadXMLData<Employee>("Employee.XML");
GridView1.DataBind();
}


#region For Serialization Code

public void SaveXMLData<T>(List<T> list, string fileName)
{
XmlSerializer serializer = new XmlSerializer(typeof(List<T>));
{
TextWriter writer = new StreamWriter(Server.MapPath("~\\XMLFiles") + "\\" + fileName);
serializer.Serialize(writer, list);
writer.Close();

}
}

public List<T> ReadXMLData<T>(string filename)
{
XmlSerializer deserializer = new XmlSerializer(typeof(List<T>));
TextReader textReader = new StreamReader(Server.MapPath("~\\XMLFiles") + "\\" + filename);
List<T> list;
list = (List<T>)deserializer.Deserialize(textReader);
textReader.Close();

return list;
}

#endregion
}


If you see the code on page load I have save the data first into XML and then retrieve and display into the grid due to this every time save data into XML So if  you find any problem in module you will get the XML file from Production or Test Server and Load into development environment through ReadXMLData() method and debug it.



Here is the XML Generate file from the above SaveXMLData() method.



<?xml version="1.0" encoding="utf-8" ?> 
- <ArrayOfEmployee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
- <Employee>
<EmployeeName>Employee-0</EmployeeName>
<Address>Address-0</Address>
<MobilNo>9901</MobilNo>
</Employee>
- <Employee>
<EmployeeName>Employee-1</EmployeeName>
<Address>Address-1</Address>
<MobilNo>9902</MobilNo>
</Employee>
- <Employee>
<EmployeeName>Employee-2</EmployeeName>
<Address>Address-2</Address>
<MobilNo>9903</MobilNo>
</Employee>
- <Employee>
<EmployeeName>Employee-3</EmployeeName>
<Address>Address-3</Address>
<MobilNo>9904</MobilNo>
</Employee>
- <Employee>
<EmployeeName>Employee-4</EmployeeName>
<Address>Address-4</Address>
<MobilNo>9905</MobilNo>
</Employee>
</ArrayOfEmployee>




Scenario 2



If you are thinking for every time save and read data from XML then application performance will affected so you can change the code like this only when exception will raise it will save the data into xml and then if you want to debug write ReadXMLData() method in a code where you want.



protected void Page_Load(object sender, EventArgs e)
{
try
{
Employee objEmp = new Employee();
GridView1.DataSource = objEmp.GetEmployees();
GridView1.DataBind();
}
catch (Exception ex)
{
SaveXMLData(objEmp.GetEmployees(), "Employee.XML");
throw;
}

}



Conclusion






We have seen in above samples that how we can take advantage of serialization mechanism provided by .Net. In this tutorial I have tried to keep things as simple as possible so you can understand and using the basic concepts of XML serialization for debugging the application. Feel free to share your comments.

No comments:

Post a Comment

PDF Arabic watermark using MVC and iTextSharp

PDF full page Arabic watermark using MVC and iTextSharp Download :  Source Code Most of the time we have requirement to  gen...