Posts

Donate

Using Microsoft.VisualBasic DateDiff() Function In Your C# Program

Below is a snippet on how to use the Microsoft.VisualBasic DateDiff() Function in your C# program. Add the Microsoft.VisualBasic reference to your project so that you can access it's underlying classes and methods. long age = Microsoft.VisualBasic.DateAndTime.DateDiff( "yyyy" ,Convert.ToDateTime( "1985-04-04" ),DateTime.Now);

How To Decode An Encoded URL In C#.NET

The code below illustrates how to decode an encoded url using HttpUtility.UrlDecode(string url) 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);

Read Or Parse XML Using XmlElement Class, XPath And XmlNodelist In C#

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 = elem

(502)Bad Gateway Returned From HttpWebRequest In C#

The solutions is to set the default proxy for the web request and assign user agent to it as shown below: ListURL = String.Format( "your_xml_feed_or_API_url_sample" ); HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(ListURL); request.Timeout = 900000000; request.Proxy = HttpWebRequest.DefaultWebProxy; request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)" ; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream dataStream = response.GetResponseStream(); dataStream = response.GetResponseStream(); StreamReader reader = new StreamReader(dataStream); ListingSource = reader.ReadToEnd(); ListingSource = System.Web.HttpUtility.HtmlDecode(ListingSource); Cheers!

Invalid Expanded Name In LINQ to XML

Assuming I am reading this xml file using XLINQ. I encountered an error that says invalid expanded name. As i go through this xml file, i found a node images that contains element tags. The parent node is also an element tag. Since i don't use the images tag, I simply remove it using string manipulations. Below is the xml file and the solution: <element> <id> 2768687195 </id> <title> Customer Service Representative </title> <body> Do you dream of being your own boss and choosing the work you do, when you work and who you work for? </body> <url> http://jobs.myjob.careercenter-servicecrew.html </url> <category> <id> job/customer_service </id> <name> Customer Service &amp; Call Center Jobs </name> </category> <source> <id> www </id>

How To Read Or Parse XML Using LINQ And XLinq In C#

Hello, Here's an example on how to read or parse XML file using LINQ and XLINQ in C#. XML Content: <item> <title> Labor Warehouse Associates - Boston, Massachusetts </title> <link> http//www.BostonRecruiter.com/job_display.php?alpha=17343491 </link> <description> Hudson Group is the #1 airport retailer operating in over 400 retail locations in most major airports, throughout the US and Canada. While we are comprised of many </description> <guid> http//www.BostonRecruiter.com/job_display.php?alpha=17343491 </guid> <joblocation> <jobcity> Boston </jobcity> <jobstate> Massachusetts </jobstate> <jobcountry> United States </jobcountry> </joblocation> </item> C# Code: string url = "your xml or rss link" ; string xmlsource = client.DownloadString(url);

Manipulate Object Property Using Lambda Expression In LINQ

Here's how to manipulate or update an object property using Lambda format in LINQ. In this example, I assigned a value to the textbox control's Text property using a delegate that applies the concept of Lambda expressions in LINQ. ShowEmployeeDelegate d = () => { tMessage.Text = "Nelson" ; }; d.Invoke();

Remove Open And Close Square Brackets In A Sentence Using Regular Expression In C#

Given the sample string data [Posted Friday September 23 2011] and you need format the sentence like this Posted Friday September 23 2011 without the square brackets, a solution would be to use Regex. string date = Regex.Replace(date, "[\\[\\]]" , string .Empty); date = Regex.Replace(date, @"[\[\]]" , string .Empty);

Replace Character In A String Based On Specific Index In C#

Here's an example snippet that will replace a character based from a specific index. Source: DreamInCode To use it in your program do it like this: // Replaces a character in a string with the character specified. void replaceCharWithChar( ref string text, int index, char charToUse) { char [] tmpBuffer = text.ToCharArray(); buffer[index] = charToUse; text = new string (tmpBuffer); } //IN YOUR MAIN MODULE/FUNCTION. //money has comma, example: $2,000, 4 BR, 180 m² //LandPriceBedroom = "$120, 2 BR, 90 m²"; //IF COMMA OCCURS MORE THAN TWO, REPLACE THE FIRST //COMMA WITH SPACE. SINCE IT COULD BE A MONEY W/ //THOUSANDS SEPARATOR if (LandPriceBedroom.Split( ',' ).Length > 3) { int firstIndexComma = LandPriceBedroom.IndexOf( "," ); replaceCharWithChar( ref LandPriceBedroom, firstIndexComma, ' ' ); }

Regular Expression Remove String With A Punctuation Beside It In A Sentence In C#

Assuming in a sentence, you want to remove the word apply now in any case. This word has a punctuation beside it. Example: Apply now! Apply now. APPLY NOW! APPLY NOW. apply Now! apply Now. In a sentence: We are in need of VB.NET Developers. Apply Now! We are in need of VB.NET Developers. Apply now! We are in need of VB.NET Developers. APPLY NOW! We are in need of VB.NET Developers. apply now! To remove, just use the code below: description = Regex.Replace(description, "apply now[!.]" , string .Empty,RegexOptions.IgnoreCase).Trim(); Note: I'm not a Regex expert.

Donate