Monday, November 21, 2011

How to Decode and encoded URL in .NET

The code below illustrates how to decode an encoded url using HttpUtility.UrlDecode(string url)
Code:
  using System.Web;   
  //make sure to add reference to System.Web  
  string temp_url = "http%3a%2f%2fplacement.emploiquebec.net%2f"  
        + "mbe%2fut%2fsuivroffrs%2fapercoffr.asp"  
        + "%3fnooffr%3d3053675%26page%3drech%26prov%  
  3derechroffr%252Easp%26CL%3denglish";  
  string url = HttpUtility.UrlDecode(temp_url); 

Wednesday, November 9, 2011

Read or parse XML using XmlElement class and XmlNodelist

Sample xml file:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
<?xml version="1.0" encoding="UTF-8"?>
<jobs>
   <job>
      <title>Payroll Analyst</title>
      <description>
         <summary>Seeking Payroll Analyst specialists.    
  Great benefits and competitive salary. Must meet all requirements    
  to be considered for assignment.</summary>
      </description>
      <location>
         <state>Pennsylvenia</state>
      </location>
   </job>
   <job />
</jobs>
Code:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
ListingSource = webclient.DownloadString("your_xmlurl");  
StringReader readString = new System.IOStringReader(ListingSource);  
XmlDocument awesome = new XmlDocument();  
awesome.Load(readString);  
XmlNodeList nodeList = awesome.SelectNodes("//jobs/job");  
foreach (XmlElement element in nodeList)  
{  
    title = element["title"].InnerText.Trim();  
    state_name_text = element["location"]["state"].InnerText.Trim();   
    description = element["description"]["summary"].InnerText.Trim();  
}