XmlException: Data At The Root Level Is Invalid

Friday, April 18, 2008 by Mistlee


Can't see any images? -!



Visit The XML Pro News Directory
CSS
Templates, Tag Reference
News
Articles, Books
XML Articles
Blogs, Recent News
XML Consultants
Consulting Networks, Training
XML Editors
XML Text Editors, XML WYSIWYG Editors
XML Encoding
Tags, Rules
XML Layouts
Code Layout, Page Layout
XML Programming
Methods, Applications

Submit your site for FREE

Recent Articles

XML Sitemap Ping Tool
All the major search engines (Google, Yahoo!, MSN/Live, and Ask) use the XML Sitemaps protocol for getting URLs from websites. Of course they all still use good old-fashioned crawling, but the XML sitemap can be...

XML-RPC ping endpoint in C# and ASP.NET
All blog platforms send out pings using the XML-RPC protocol whenever a new post is created or an old one is updated. It is very simple to send out XML-RPC...

A New Way to Organize Your Feeds
When you come across something interesting on the web, but don't have time to read it at that moment, what do you do?The old way is to add the web page to your...

Google Sitemaps and Competitive Intelligence
I'm a big fan of the Google Webmaster Central Program and using sitemaps. I agree that you should build your website so that it is crawlable and not rely on sitemaps...


04.18.08

XmlException: Data At The Root Level Is Invalid

By Mads Kristensen

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:



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:



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.

Comments


About the Author:
Mads Kristensen currently works as a Senior Developer at Traceworks located in Copenhagen, Denmark. Mads graduated from Copenhagen Technical Academy with a multimedia degree in 2003, but has been a professional developer since 2000. His main focus is on ASP.NET but is responsible for Winforms, Windows- and web services in his daily work as well. A true .NET developer with great passion for the simple solution.
About xmlProNews

xmlProNews is a collection of news and commentary designed to keep you in step with the ever evolving landscape of XML environments. News and Advice for XML Professionals





xmlProNews is brought to you by:

SecurityConfig.com NetworkingFiles.com
NetworkNewz.com WebProASP.com
DatabaseProNews.com SQLProNews.com
ITcertificationNews.com SysAdminNews.com
LinuxProNews.com WirelessProNews.com
CProgrammingTrends.com SysAdminNews.com






-- XMLProNewsis an iEntry, Inc. publication --
iEntry, Inc. 2549 Richmond Rd. Lexington KY, 40509
2008 iEntry, Inc.  All Rights Reserved  Privacy Policy  Legal

archives | advertising info | news headlines | free newsletters | comments/feedback | submit article


Unsubscribe from XMLProNews.
To unsubscribe from XMLProNews or any other iEntry publication, simply send an email request to: support@ientry.com
XMLProNews

0 comments: