A few days ago I needed to write some functionality to fetch an XML document from a URL and load it into an XmlDocument. As always I use the WebClient to retrieve simple documents over HTTP and it looked like this:

using (WebClient client = new WebClient())

{

  string xml = client.DownloadString("http://example.com/doc.xml");

  XmlDocument doc = new XmlDocument();

  doc.LoadXml(xml);

}

I ran the function and got this very informative XmlException message: Data at the root level is invalid. Line 1, position 1. I’ve seen this error before so I knew immediately what the problem was. The XML document that was retrieved from the web had three strange characters in the very beginning of the document. It looks like this:

<?xml version="1.0" encoding="utf-8"?>

Of course that result in an invalid XML document and that’s why it threw the exception. The three characters are actually a hex value (0xEFBBBF) of the preample of the encoding used by the document.

As said, I knew this error and also an easy way around still using the WebClient. Instead of retrieving the document string from the URL and load it into the XmlDocument using its LoadXml method, the easiest way is to retrieve the response stream and use the Load method of the XmlDocument instead. It could look like this:

using (WebClient client = new WebClient())

using (Stream stream = client.OpenRead("http://example.com/doc.xml"))

{     

  XmlDocument doc = new XmlDocument();

  doc.Load(stream);

}

Often there are situations where the WebClient isn’t well suited for this or one might simply prefer to use the WebRequest and WebResponse classes. Still, the solution is very simple. Here is what it could look like:

WebRequest request = HttpWebRequest.Create("http://example.com/doc.xml");

using (WebResponse response = request.GetResponse())

using (Stream stream = response.GetResponseStream())

{

  XmlDocument doc = new XmlDocument();

  doc.Load(stream);

}

This is something that can give gray hairs if you haven’t run into it before, so I thought I’d share.  

If you have any issues with the three preample characters when serving - not consuming - XML documents, then check out Rick Strahl's very informative post about it.

Gravatar is a service that associates your e-mail addresses with a picture of you for use on blog comments, forums and bulletin boards. I’ve been a happy user for years.

A cool thing about Gravatar images is that they can be used from any application either on a website or a desktop application. But still, they are primarily used on blogs and forums.

Today, I played with the thought of a wider use and wrote some code that makes working with Gravatars very easy on both web and the desktop. The reason for this is that I needed a way to collect Gravatar images for a list of users, but only the users that actually have a Gravatar associated with their e-mail address.

The Gravatar service doesn’t have an API you can use to check if an e-mail address is associated or not. But by downloading a Gravatar image, the response header Last-Modifed is 1970-01-01 if no image is found and the default image is returned. This information can be used to only store the images users have uploaded to the Gravatar service.

An example

The class is called Gravatar and is very simple and small. Here is an example of using the class to download a Gravatar image and save it to disk.

string email = "name@example.com";

Gravatar gravatar = new Gravatar(email);

 

if (!gravatar.IsDefaultImage)

{

  using (FileStream writer = new FileStream("c:\\" + email + ".jpg", FileMode.Create))

  {

    writer.Write(gravatar.Image, 0, gravatar.Image.Length);

  }

}

Download

Download the class below and put it in your App_Code folder. Then you can use the example above from any ASP.NET page or control.

Gravatar.zip (888,00 bytes)