Deserializing XML twitter feeds.
July 2nd, 2010
No comments
1. Use xsd.exe to extract an xsd file from an xml feed.
Download a private xml feed for example from “http://api.twitter.com/1/statuses/user_timeline.xml” and store it to local disk. Use xsd.exe to generate the xsd.
2. Generate the classes from the xsd file you just generated.
xsd.exe yourxsd.xsd /c3. Add the generated classes to your C# project.
4. Time for some fetching here is a short example:
// encode the username/password string user = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(username + ":" + password)); HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://api.twitter.com/1/statuses/user_timeline.xml"); // set the method to GET request.Method = "GET"; request.ServicePoint.Expect100Continue = false; // set the authorisation levels request.Headers.Add("Authorization", "Basic " + user); request.ContentType = "application/x-www-form-urlencoded"; using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) { XmlSerializer serializer = new XmlSerializer(typeof(statuses)); statuses s = serializer.Deserialize(response.GetResponseStream()) as statuses; listBox1.Items.Clear(); foreach (statusesStatus status in s.status) { listBox1.Items.Add(status.text); } }