IP Address validation in C#

In this post, I have used Regular expression to validate if the IP address provided as string is valid IP Address or not.

Usefull when we want user to input IP address in our application and we want to check if the IP address is corrct format or not.

public bool IsValidIP(string addr)
//create our match pattern
string strPattern = @”^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}$”;
//create our Regular Expression object
Regex check = new Regex(strPattern);
//boolean variable to hold the status
bool valid = false;
//check to make sure an ip address was provided
if (addr == “”)
//no address provided so return false
valid = false;
//address provided so use the IsMatch Method
//of the Regular Expression object
valid = check.IsMatch(addr, 0);
//return the results
return valid;

kick it on DotNetKicks.com

Parsing an XML file from RSS feed with namespaces in C#

The task is to parse a XML file retrieved from the RSS feed.
Below is the sample partial XML file taken from a Leading Geographical alerts website’s RSS feeds.

<?xml version="1.0" encoding="ISO-8859-1"?> 
<rss version="2.0" xmlns:geo="http://www.w3.org/2003/01/geo/"> 
<pubDate>Fri, 4 Jan 2008 09:51 GMT+1</pubDate> 


<title>Red alert: Tropical Cyclone SINLAKU-08.</title> 
<description>Tropical Cyclone SINLAKU-08 .</description> 
<pubDate>Tue, 16 Sep 2008 06:00 GMT+1</pubDate> 

<title>Red alert: Tropical Cyclone IKE-08 .</title> 
<description>Tropical Cyclone IKE-08 .</description> 
<pubDate>Sun, 14 Sep 2008 09:00 GMT+1</pubDate> 


As we can we there are two items(alerts) in the file one for Tropical Cyclone SINLAKU-08 and other for Tropical Cyclone IKE-08.

Lets write the code in c# to extract title,latitude and longitude of both the alerts.

First of all, lets load the XML data into the XMLDocument instance.

//include namespace for working with xml files
using System.Xml;
public partial class GoogleMap : System.Web.UI.Page
//creating new instance of xml document
XmlDocument xmlDoc = new XmlDocument();
protected void Page_Load(object sender, EventArgs e)
xmlDoc.Load(Server.MapPath(“~/XML Files/sourceRssEDIS.xml”));
//rest of the code described later will also be added here

Now, introduce the namespaces used in the xml file to the xmlDoc with the XmlNamespaceManager class instance.

XmlNamespaceManager nsMgr = new XmlNamespaceManager(xmlDoc.NameTable);
nsMgr.AddNamespace(“geo”, “http://www.w3.org/2003/01/geo/”);

Select all the items in a array using GetElementsByTagName() method.

XmlNodeList nodes = xmlDoc.GetElementsByTagName(“item”);

Iterate for the elements inthe nodes array using for loop and select the latitude and longitude values using SelectNodes() method as shown below and then set the values of text of respective labels.

for (int i = 0; i < nodes.Count; i++) { XmlNodeList gpointnodeslat = nodes[i].SelectNodes("geo:point//geo:lat", nsMgr); XmlNodeList gpointnodeslong = nodes[i].SelectNodes("geo:point//geo:long", nsMgr); XmlNodeList titleNodes= nodes[i].SelectNodes("title"); Label lbllatitude=new Label(); Label lbllongitude = new Label(); Label lbltitle = new Label(); latitude.Text = gpointnodeslat[0].InnerText; longitude.Text = gpointnodeslong[0].InnerText; lbltitle.Text = titleNodes[0].InnerText; } [/sourcecode] In this way, any tag in the xml file can be searched for which is either with a namespace or not. kick it on DotNetKicks.com

New Way to Pass Querystrings into Dynamically Loaded Usercontrol in ASP.NET


How to pass values into a usercontrol as querystrings in asp.net.
More References to the problem as as follows:


  1. One Can definitely use Session Variables to give the functionality. Set the session variable value in the page and then retrieve the value in the user control.
  2. Another way is to set the Public Property for the userControl.
  3. And one solution is described below which i implemented successfully. It also works for dynamically loaded usercontrol.

The Alternative New Method:

Method is simple:

  1. Add Invisible buttons(or label) to the user control on the page where it will be rendered.
  2. Set the text property of the buttons to the value of the querystring.

    UserControl mycontrol = new UserControl();
    mycontrol = (UserControl)Page.LoadControl(“./UserControls/CustomCallDetailsChart.ascx”);

    Button btnName=new Button();
    btnName.Visible = false;
    mycontrol.Controls.AddAt(0, btnName);


  3. In the page load event of the usercontrol find the control and typecast the text value to the required datatype.
    protected void Page_Load(object sender, EventArgs e)
    Button btnName = (Button)this.Controls[0];
    this.ChartName = btnName.Text.ToString();

kick it on DotNetKicks.com