Parsing XML using C#

Consider you have to extract XML based information from a given URL and display it on the web page. This is really useful if you do not want to use iframes and have to customised the appearance of the final output.

Assume we have agreed that the information will look like something as shown in XML below:

<?xml version="1.0" encoding="UTF-8"?> <list> <item1>some item 1</item1> <item2>some item 2</item2> </list>

And some information provider is feeding this information from a URL

http://www.samplelist.com/list

Here is how to go about extracting and displaying it.

Step 1: Create a Literal control on the .aspx or .ascx file and set the ‘Text’ property as the URL and ‘Visible’ property as false

<asp:Literal ID="LiteralURLTrackerAddress" Text="http://www.samplelist.com/getlist" Visible="false" runat="server"></asp:Literal>

You can also add it to web.config if the same URL is used else where in the site and reference it, but please do not hard code it.

Step 2: Add two more Literal controls for each of the list items as below:

 <asp:Literal ID="LiteralItem1" runat="server"></asp:Literal> <asp:Literal ID="LiteralItem2" runat="server"></asp:Literal>

As they are literal controls, you can add any CSS around these controls and set the appearance.

Step 3: In the code behind file, .aspx.cs or .ascx.cs,  add the following code in Page_Load()  or OnLoad() function

Step 4: First we need to create a Request object from the Literal’s text that we set up in step 1

 //create HTTP request
 string URLToSend = LiteralURLTrackerAddress.Text.ToString();
 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URLToSend.ToString()); request.Method = "GET"; request.KeepAlive = true;

Step5: Get a response from the request object

// Get response for http web request
 HttpWebResponse webResponse = (HttpWebResponse)request.GetResponse();

Step 6: Read the response in StreamReader

if (webResponse != null) { //Xml response read from a stream; StreamReader responseStream = new StreamReader(webResponse.GetResponseStream());
//get the response string webResponseStream = responseStream.ReadToEnd();

Step 7: As we have agreed that it will be XML format,  parse the response to XML

//parse it into XML
 XmlDocument listXML= new XmlDocument(); listXML.LoadXml(webResponseStream);

Step 8: Read the individual nodes,  your code logic may be different from here onwards and will depend upon your own XML schema.

string baseNode = @"list/"; //basenode
 //actual nodes XmlNode item1 = listXML.SelectSingleNode(baseNode + "item1"); XmlNode item2 = listXML.SelectSingleNode(baseNode + "item2");

Step 9: Assign them to the Literals

 //assign them back LiteralItem1.Text = item1.InnerText.ToString(); LiteralItem2.Text = item2.InnerText.ToString();

Compile and remove any errors.

Add a try / catch block around the code to make it more robust.

Advertisements

Configuring video settings in EPiServer Relate+

I have recently worked on a project using standard out-of-box EPiServer Relate+ website and templates. As every module is very well written and integrated, the only missing piece was the video upload settings.

It took me a while to find out how to get these settings and update the site so that end user can upload video.  I thought it will be a good idea to write up a small blog for fellow developers.

Step 1. Navigate to http://world.episerver.com/System/Video-Service/ and login with your usual world.episerver account

Step 2. Select register account as shown below

register video

Step 3. The next screen will show your information, make sure all the required fields are there and click ‘Next’ (I have removed my auto generated email address)

video next

Step 4:  Read and Check the ‘Terms & Condition’ box. As I am using it for demo purposes, I would select the development license.

video confirm

Step 5: If your account is successfully created, you would be shown  <cdnProvider>  settings at the bottom of the page.

Step 6: Open your EPiServer Relate+ web.config and find the <cdnProvider> node and replace the accessKey and secretAccessKey values with the actual values generated at the end of the process.

Navigate to the actual site and try uploading any video. The video service modules converts all movies into flash format so it may take a while to upload your video and it will depend upon your system, network traffic and video file size.

Secondly, video files may not appear instantly on the page so just be patient.

Cheers

iPhone development for a .NET developer

If you are a .NET developer (like me) and would like to explore the iPhone world, then this is the post for you.

The first thing you need to do is under stand the Cocoa framework which is Apple’s equivalent of .NET framework, just google for it and get yourself familiar with the concepts of views, windows, controllers, apis etc.

The next thing is to get familiar with Objective-C, which is a C/C++ kind of language but uses SmallTalk syntax for whatever reason best known to Objective-C development team.

The most important thing is that there is no  garbage collections / memory management for declared variables or objects. Once you are finished with the object/view, you need to de allocate the memory.

Once youare happy with that, get yourself on iMac and start coding.  There are plenty of tutorials / webcasts available on YouTube, check them out as your starting point.

I will be posting a step by step guide ‘how to implement a simple temperature conversion’ iphone app and comparing it side by side with a similar C# app to clear the differences.

Normalizing postcode in C#

In many web applications, you need to ask user a postcode to display list of addresses so that user can select the relevant one.

User can enter in any format like as shown below (and they all are valid ones)

M1 1AA or M11AA

M60 1NW or M601NW

EC1A 1BB or EC1A1BB

so how to go about sending it in the right format to fetch data?

Simple string functions like Trim(), Replace() and Insert() would come handy.

Consider the following function

Explanation of the code:
1. It trims the postcode that is remove any unwanted starting or ending blank spaces

2. It would remove any spaces enter in the middle

3. So now we have a normalized postcode, which we could manipulate.

4. Using a switch on length of string, we could insert a blank space where ever we want

5. Returns the postcode that we want.

The code is given in C# but I am sure it can be adopted in any lanugage, and I hope my Sat Nav people see this post!

Calculating age from date of birth – C#

This is a really small snippet of code that I have used over and over again.

Imagine you are given date of birth of the person and you have to get the age, how would you go about it?

1. Create a private function and pass it date of birth

2. Subtract the number of years from current year

3. In C#, every day of the year has got a value, an extra check is required to see if the current Day is less than the day the person was born in, for example, if today is 15th of March and the person was born 1st of March, you have to subtract an extra year.

Sample code is given below