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.

At ZYB we test SyncML synchronization of just about every cell phone on the market, so we can optimize the sync process. That’s because phone manufactures have very different ways to support SyncML to say the least. It also means we get our hands on a lot of cool devices from time to time – and some not so cool ones too.

Today, I was so lucky to get my hands on the brand new LG KU990 Viewty. It is a Symbian smartphone with a huge 3” touch screen and no keyboard. It also has a 5 mega pixel camera with flash and auto focus.

I was a big sceptic to begin with about the touch screen, since I’ve only seen it work properly on the iPhone. My fear was that it would be a hassle to write text messages or perform just the basic operations like it was on the HTC Touch.

After playing around with it all night I must say that I’m very impressed with the responsiveness and precision of the screen. It just works. Sometimes though, my fingers are too big for some small icons and the stylus that is bundled in the box is just not a thing I would like to carry around with my phone. That being said, it is a rare case when the stylus is needed – I have no plans to use it not even once.

There’s nothing like a new gadget.