Creating a Twitter Client in C#, Part 2

Posted by Matt | Filed under , , , ,

In my previous article, Creating a Twitter Client in C#, I showed you the basics of creating a Twitter client using C# and WCF.

After doing some investigation, it turns out that for some reason, C# WCF does not want to correctly parse some of the Xml data returned. For example, if we add profile_image_url to the User class

class User
{
  [DataMember(Name = "profile_image_url")]
  public string ProfileImageUrl { get; set; }
}

and then call our VerifyCredentials method, the profile_image_url parameter always ends up null after it’s parsed from the Xml data.

However, if we switch from Xml to Json formatted data, these fields do correctly get parsed.  To do the switch, just change the following in our ITwitterClient interface

[OperationContract]
[WebGet(UriTemplate = "/account/verify_credentials.xml",
  BodyStyle = WebMessageBodyStyle.Bare,
  RequestFormat = WebMessageFormat.Xml,
  ResponseFormat = WebMessageFormat.Xml)]
User VerifyCredentials();

to

[OperationContract]
[WebGet(UriTemplate = "/account/verify_credentials.json",
  BodyStyle = WebMessageBodyStyle.Bare,
  RequestFormat = WebMessageFormat.Json,
  ResponseFormat = WebMessageFormat.Json)]
User VerifyCredentials();

Now it retrieves the data correctly.

From here on, we’ll be using the Json format instead of Xml for the client.

If anyone has any ideas on why it’s failing reading the Xml data, I’d be very interested in hearing them.

Comments are closed