Friday, April 24, 2009

Don't use XmlDocument use XmlTextReader instead

Hi,
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" ?>

<RootNode>

<Child>

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

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

</Child>

<Child>

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

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

</Child>

</RootNode>



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))
return;

//@@@ 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
reader.MoveToAttribute("Att");
sAttribute = reader.Value;
Console.WriteLine(sAttribute);
//@@@ Get the node value
reader.MoveToElement();
sNodeValue = reader.ReadInnerXml();
Console.WriteLine(sNodeValue);
}
}
}

Tuesday, April 21, 2009

The perfect c# singleton

Hi,
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
{
get
{
if (DbProxy._oInstance == null)
{
lock (padlock)
{
if (DbProxy._oInstance == null)
{
DbProxy newDB = new DbProxy();
System.Threading.Thread.MemoryBarrier();
DbProxy._oInstance = newDB;
}
}
}
return DbProxy._oInstance;
}
}
// @@@ private constructor
private DbProxy()
{

}
}


Monday, April 13, 2009

Output Cache by the book

Hi,
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:

<system.web>

<caching>

<outputCacheSettings>

<outputCacheProfiles>

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

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

</outputCacheProfiles>

</outputCacheSettings>

</caching>

</system.web>


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
TEST.aspx

<%@ OutputCache CacheProfile="profile1" %>

<html xmlns="http://www.w3.org/1999/xhtml" >

<body>

<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 />

</form>

</body>

</html>


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

Hi,
with this line u can create a permanent breakpoint in c# applications:
System.Diagnostics.Debugger.Break();

recomanded and very useful

Wednesday, April 8, 2009

Send array of objects form client to server

Hi,
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)

THE SERVER CODE:

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();
person.name = 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 > >
(hiData.Value);
}
}
public class Person
{
public string name;
public int age;
}



THE CLIENT CODE:

<%@ 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);
tempP.name = 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));
}
</script>
<form id="form1" runat="server">
<div>
<input onclick="add2Items()" type="button" value="Create Array">
<input id="hiData" type="hidden" runat="server">
</div>
<asp:linkbutton id="LinkButton1" onclick="LinkButton1_Click" runat="server">Send To Server</asp:linkbutton>
</form>