Thursday, August 23, 2007

XML & XPath Expression

When we have an application which is having XML Files as the data storage and need the fastest way to only read the data, XPath will be a good solution.

How to loop the data from an xml into XPath is quite simple.
I will be using .Net code below to do this.

XPathDocument xpDoc = new XPathDocument(@"C:\Temp\data.xml");
XPathNavigator xpNav = xpDoc.CreateNavigator();
XPathNodeIterator xpIter = xpNav.Select("/root/row");
while (xpIter.MoveNext())
Console.WriteLine(xpIter.Current.GetAttribute("id", string.Empty));

The code will simply load the data.xml and then loop the data and write the ID to the console.

There will be occasions where we need to perform more complex queries to the data which we can do it quite simple such as in the SQL statement below:

Select * From data

Where colA In ('AA', 'ZZ')
And colB = 'X'
And colC Like '%123%'

We can achieve the same result with XPath by using this expression below :

XPathNodeIterator xpIter = xpNav.Select("/root/row[(@colA = 'AA' or @colA = 'ZZ') and @colB = 'X' and contains(@colC, '123')]");

There is a lot of ways that we can use in XPath, you can find the complete references in

XML is always an interesting type of data that we can use, there is a lot of database technologies nowadays which has actually supports XML type of data such as SQL2005 where I heard we can set the indexes in the XML type of field ;)

Unicode content in Cookies

This is another small small things but can get to the nerves ;)

So basically, I was doing some changes with a Taiwan web application, previously it was using Session to store some information and right now because of we're integrating with other application, we need to use the cookie instead so the information can be shared.
Note: When using cookies, if the information is confidential, you may need to consider encryption.

After i modified the application and tested it in one of the server, everything is working perfectly, until I tried it in the other server, just to make sure that this is working fine as well. And the unexpected actually happened, the unicode characters were corrupted, they are becoming question marks (????).
Firstly I thought that the server probably missing some language pack, and then I installed the language pack to the server and turned out, it was not helping either. Then I tried this application into another server again (it is fun to have a lot of servers to play and test with hohohoho), and some of them were able to save the unicode correctly and some of them didn't, including my own PC :(

I was kept thinking, what is the difference between these machines, why some of them are working and some of them aren't. I looked into the cookie file in the my local machine, the cookies which were saved correctly are having UTF-8 as the encoding and the cookies which are corrupted were having ANSI as the encoding.

So in the end, actually instead of thinking why UTF-8 and ANSI, there is a simple solution.
Use HttpUtility.UrlEncode when saving cookie, and use HttpUtility.UrlDecode when reading back from the cookie.
Using this way, we don't need to worry whether the characters will be corrupted because of some machine settings :)

Sounds easy rite? hhhmm... it took me almost 2 days browsing and browsing and then have this solution come to my mind. Getting older I guess hahaha ;)