Friday, April 24, 2009

Don't use XmlDocument use XmlTextReader instead

I had an assignment to read a 200 MB xml file
i dont think that XmlDocument can help me here...
so i found the XmlTextReader object that made a great job
and from this moment i always use XmlTextReader to read
xml file content, so here is my example:

lets say that i want to read this Test.xml file:

<?xml version="1.0" encoding="utf-8" ?>



<Item Att="1">A</Item>

<Item Att="2">B</Item>



<Item Att="3">C</Item>

<Item Att="4">D</Item>



and this function read all the attributes and content
of the Item node in the xml:

public void ReadXml()
int iCodepage = 862;
string sAttribute, sNodeValue;
string sFilePath = @"Test.xml";
StreamReader oFileStream = null;
XmlTextReader reader = null;
//@@@ Check if the file exists
if (!File.Exists(sFilePath))

//@@@ Get the file as XmlTextReader
oFileStream =
new StreamReader(sFilePath, Encoding.GetEncoding(iCodepage));
reader = new XmlTextReader(oFileStream);

//@@@ Loop through all the file
while (reader.Read())
//@@@ If we in Item node
if (reader.Name.Equals("Item") &&
(reader.NodeType == XmlNodeType.Element))
//@@@ Get the Attribute (Att) value
sAttribute = reader.Value;
//@@@ Get the node value
sNodeValue = reader.ReadInnerXml();

Tuesday, April 21, 2009

The perfect c# singleton

after several tries i got the perfect thread safety c# singleton

public class DbProxy
private static DbProxy _oInstance = null;
private static readonly object padlock = new object();

// @@@ public "consructor"
public static DbProxy Instance
if (DbProxy._oInstance == null)
lock (padlock)
if (DbProxy._oInstance == null)
DbProxy newDB = new DbProxy();
DbProxy._oInstance = newDB;
return DbProxy._oInstance;
// @@@ private constructor
private DbProxy()


Monday, April 13, 2009

Output Cache by the book

i found the best way to implement Output Cache
we have only 2 steps to make it:

step 1
define in the Web.config some different cache profiles that u need:





<add name="profile1" duration="100" varyByParam="param1" />

<add name="profile2" duration="100" varyByParam="None" />





step 2
define in your pages the profile that u want to use:

<%@ OutputCache CacheProfile="profile1" %>

now, if we want to check if it works
all we have to is to make aspx page, and to play with his links
and check if the DateTime changed or not

<%@ OutputCache CacheProfile="profile1" %>

<html xmlns="" >


<form id="form1" runat="server">

<%= DateTime.Now.ToString() %><br />

<a href="?param1=1">1</a><br />

<a href="?param1=2">2</a><br />

<a href="?param1=3">3</a><br />




Another small tip:
for disable the OutputCache(in the development environment for example) add location="None" in the appropriate profile in the web.config in this way:

<add name="profile1" duration="100" varyByParam="param1" location="None" />

Saturday, April 11, 2009

A new startup - create a permanent breakpoint

with this line u can create a permanent breakpoint in c# applications:

recomanded and very useful

Wednesday, April 8, 2009

Send array of objects form client to server

with this code u can send array of object form client to server in c#
u have to copy the client and server code from this post
and use this 2 dependencies:
1. JSON Code(Take js file)
2. Serialize Code(Take dll file)


using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Newtonsoft.Json;
using System.Collections.Generic;

public partial class tbl1 : System.Web.UI.Page
public string tempPerson;
public string temp_arr;
public List person_arr = new List< person >();
protected void Page_Load(object sender, EventArgs e)
Person person = new Person(); = string.Empty;
person.age = 0;
string json1 = JavaScriptConvert.SerializeObject(person);
tempPerson = json1;
string json = JavaScriptConvert.SerializeObject(person_arr);
temp_arr = json;
protected void LinkButton1_Click(object sender, EventArgs e)
person_arr = JavaScriptConvert.DeserializeObject< List< person > >
public class Person
public string name;
public int age;


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Arr.aspx.cs" Inherits="tbl1" %>
<script src="json2.js" type="text/javascript"></script>
<script type="text/javascript">
var arr = <%=temp_arr%>;
var p1 = <%=tempPerson%>;
var hiDataID = '<%=hiData.ClientID%>';
function add_to_arr(name, age)
var tempP = clone(p1); = name;
tempP.age = age;
arr[arr.length] = tempP;
function add2Items()
add_to_arr("name1", 1);
add_to_arr("name2", 2);
var g = JSON.stringify(arr);
document.getElementById(hiDataID).value = g;
function clone(obj)
return JSON.parse(JSON.stringify(obj));
<form id="form1" runat="server">
<input onclick="add2Items()" type="button" value="Create Array">
<input id="hiData" type="hidden" runat="server">
<asp:linkbutton id="LinkButton1" onclick="LinkButton1_Click" runat="server">Send To Server</asp:linkbutton>