Wednesday, October 26, 2011

Image rendered in browser appears to be corrupt or encrypted

Here is the url that contains the images list:Image List
A sample image url that appears to be encrypted or corrupt: Image File
Solution: Change it's file extension to jpeg.
Image File

(502)Bad Gateway returned from web request

The solutions is to set the default proxy for the web request and assign user agent to it as shown below:
Code:
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!

Tuesday, October 25, 2011

invalid expanded name (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:
1:  <element>  
2:        <id>2768687195</id>  
3:        <title>Customer Service Representative</title>  
4:        <body>Do you dream of being your own boss and choosing the work you   
5:  do, when you work and who you work for?   
6:        </body>  
7:        <url>http://jobs.myjob.careercenter-servicecrew.html</url>  
8:        <category>  
9:          <id>job/customer_service</id>  
10:          <name>Customer Service &amp; Call Center Jobs</name>  
11:        </category>  
12:        <source>  
13:          <id>www</id>  
14:          <name>Oodle</name>  
15:        </source>  
16:        <location>  
17:          <citycode>can:on:ajax</citycode>  
18:          <name>Ajax</name>  
19:          <state>ON</state>  
20:          <country>CAN</country>  
21:          <latitude>43.8610</latitude>  
22:          <longitude>-79.0292</longitude>  
23:        </location>  
24:        <images>  
25:          <element>  
26:            <src>http://i.oodleimg.com/item/2768687195u_0s?1318626136</src>  
27:            <width>100</width>  
28:            <height>75</height>  
29:            <alt>Customer Service Representativ</alt>  
30:            <num>0</num>  
31:            <size>s</size>  
32:          </element>  
33:          <element>  
34:            <src>http://i.oodleimg.com/item/2768687195u_0m?1318626136</src>  
35:            <width>144</width>  
36:            <height>108</height>  
37:            <alt>Customer Service Representativ</alt>  
38:            <num>0</num>  
39:            <size>m</size>  
40:          </element>  
41:          <element>  
42:            <src>http://i.oodleimg.com/item/2768687195u_0l?1318626136</src>  
43:            <width>208</width>  
44:            <height>156</height>  
45:            <alt>Customer Service Representativ</alt>  
46:            <num>0</num>  
47:            <size>l</size>  
48:          </element>  
49:          <element>  
50:            <src>http://i.oodleimg.com/item/2768687195u_0x?1318626136</src>  
51:            <width>100</width>  
52:            <height>100</height>  
53:            <alt>Customer Service Representativ</alt>  
54:            <num>0</num>  
55:            <size>x</size>  
56:          </element>  
57:          <element>  
58:            <src>http://i.oodleimg.com/item/2768687195u_0j?1318626136</src>  
59:            <width>100</width>  
60:            <height>100</height>  
61:            <alt>Customer Service Representativ</alt>  
62:            <num>0</num>  
63:            <size>j</size>  
64:          </element>  
65:        </images>  
66:        <ctime>1318626135</ctime>  
67:        <paid>Yes</paid>  
68:        <revenue_score>6</revenue_score>  
69:        <user>  
70:          <id>51527855</id>  
71:          <url>http://www.oodle.com/seller/arise-virtual-solutions/51527855/</url>  
72:          <name>Arise Virtual Solutions</name>  
73:          <photo>http://i.oodleimg.com/a/img_profilephoto.gif</photo>  
74:        </user>  
75:        <attributes>  
76:          <company>Arise Virtual Solutions</company>  
77:          <fee>No</fee>  
78:          <full_time>Full Time</full_time>  
79:          <has_photo>Thumbnail</has_photo>  
80:          <job_title>Customer Service Representative</job_title>  
81:          <user_id>51527855</user_id>  
82:        </attributes>  
83:      </element>  
Code:
for (int ctr = 0; ctr < ListingSource.Length; ctr++)  
{  
  if (ListingSource.Contains("<images>"))  
  {  
     int length = (ListingSource.IndexOf("
     </images>") + 9) - ListingSource.IndexOf("
     <images>");  
     ListingSource = ListingSourceRemove  
     ListingSource.IndexOf("<images>"), length);  
     }  
} 

Thursday, October 13, 2011

Read or parse XML using LINQ (XLinq)

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>   
Code:
  string url = "your xml or rss link";  
  string xmlsource = client.DownloadString(url);  
  StringReader readString = new System.IOStringReader(xmlsource);  
  XDocument xmlDoc = XDocument.Load(readString);  
  //solution 1  
  var tutorials = from jobfield in xmlDoc.Descendants("item")  
  select new  
  {  
    jobtitle = (string) jobfield.Element("title"),  
    joburl = (string) jobfield.Element("link"),  
    desc = (string)jobfield.Element("description"),  
    city = (string)jobfield.Element("joblocation").Element("jobcity"),  
    state = (string)jobfield.Element("joblocation").Element("jobstate"),  
  };     
  //solution 2  
  var tutorials = from jobfield in xmlDoc.Descendants("item")  
  select new  
  {  
    jobtitle = jobfield.Element("title").Value,  
    joburl = jobfield.Element("link").Value,  
    desc = jobfield.Element("description").Value,  
    city = jobfield.Element("joblocation").Element("jobcity").Value,  
    state = jobfield.Element("joblocation").Element("jobstate").Value,  
  };