Chitika

Wednesday, November 21, 2012

Read XML data by attribute in C#


How to create XML document programmatically which i have shown in my previous XML article.
In this example i am going to show you, how to read data from XML document based on the value in c#. Use method XmlNode.Selec­tNodes to get list of nodes selected by the XPath expression. 
Check the  XML file.

<Products>
    <Product Category="0">LG DVD Writer</Name>
    <Product Category="1">I Ball Camera</Name>
    <Product Category="0">LG Monitor</Name>
    <Product Category="1">Creative Speakers</Name>
    <Product Category="0">Intel Mother Board</Name>
    <Product Category="1">Transcend Ram</Name>
    <Product Category="0">HCL Monitor</Name>
    <Product Category="1">Dell Laptop</Name>
    <Product Category="0">Sony DVD Writer</Name>
</Products>


To get all product nodes use XPath expression /Products/Product
To get only 1 category (to select all nodes with specific XML attribute) use XPath expression /Products/Product [@ Category ='1'].
[C#]

XmlDocument xml = new XmlDocument();
xml.LoadXml(str);  // suppose that str string contains "<Names>...</Names>"

XmlNodeList xnList = xml.SelectNodes("/Products/Product[@category='1']");
foreach (XmlNode xn in xnList)
{
  Console.WriteLine(xn.InnerText);
}

The output is:
I Ball Camera
Creative Speakers
Transcend Ram
Dell Laptop

Tuesday, November 20, 2012

Display Yes or No Instead of True or False in asp.net

In asp.net when we are working with there are many places where we get Boolean values which we need to show them for the user in user preferred format. 
Usually we will get this situation when binding  data control  with data bound templates.
For example if we have status of the customer .
To show the customer status to the user in grid we write below statement generally.
<ItemTemplate><%#Eval("Status")%></ItemTemplate>

The output for the above statement will be shown either True/ False based on the status.
But what the user expecting is Yes / No  or Active / In-Active it can be any thing which they prefer to use.
To accomplish this the above statement has to be updated as below,

<ItemTemplate><%#Eval("Status" ? "Yes": "No"%></ItemTemplate>

Based on the user preference we can write anything in place of Yes and No, as

<ItemTemplate><%#Eval("Status" ? "Active": "In-Active"%></ItemTemplate>

Sunday, November 18, 2012

ViewState Security in Asp.net

View state is one of the client side state mechanism. While developing web applications we use view state to maintain state. View state data stores at client side in hidden field. If data stored at client side then it can be hacked easily . 
If we look at view state which is available in hidden field it will be in encrypted format, which is nothing but a Base64 encoded string.
<div class="aspNetHidden"> <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKMTY1NDU2MTA1MmRk" /> </div>
The above string can be encrypted easily. There are many tools which will encrypt the above string.
Then how to make the view state secure?
You can make sure that the ViewState information is tamper-proof by using “hash codes”. You can do this by adding EnableViewStateMAC=true in your page directive. MAC stands for “Message Authentication Code”.
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeBehind="Default.aspx.cs" Inherits="AspnetSamples._Default" EnableViewStateMac="true" %>

When we use EnableViewStateMac="True", during ViewState save, ASP.NET internally uses a hash code. This hash code is a cryptographically strong checksum. This is added with the ViewState content and stored in a hidden filed. During postback, the check-sum data is verified again by ASP.NET. If there is a mismatch, the postback will be rejected.
You can see the view state which is encrypted and tamper proof ,
<div class="aspNetHidden"> <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKMTY1NDU2MTA1MmRkUkrfk7WraO3oDkbzEdzP7B3oVWpynylAgMi0Xne7Pjg=" /> </div>
Conclusion: In asp.net viewstate security is maintained by using EnableViewStateMAC=true property 

Friday, November 16, 2012

Data controls in asp.net


In asp.net we have 4 major  data controls those are
  1. Repeater
  2. ListView
  3. DataList and
  4. GridView

Repeater: 

          Repeater usually works faster because of DataReader class, which is used for read only access. DataReader is faster than DataSet or DataTable classes commonly used with GridView. It doesn't provide paging and sorting of records.

ListView:

           ListView control is newest data presentation control, introduced in ASP.NET 3.5. 
           Now, there is new ListView control which tries to provide best from both sides: speed and flexibility in design, and also a lot of features like paging, updating or deleting of records etc.

DataList:

           DataList has RepeatDirection, RepeatColumns and RepeatLayout properties, which are unique for DataList control. These properties are useful when you need to create presentation with more than one record per row.

GridView:

          GridView displays data in form of grid. Rows and columns are represented as HTML table. This is very useful if you need grid-like presentation. If you need to present data in table layout, then GridView requires a minimum of effort. In same time, if you want more customizable flow layout, GridView is not best option. Common problem when using GridView control could be large ViewState which makes page loads slower and requires more bandwidth. 

Conclusion: When we consider performance the order of these controls would be Repeater, ListView, DataList, and GridView.

Wednesday, November 7, 2012

C# Versions and features



The following diagram shows the list of most important features provided in the specific release up to c# 5.0.