| 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. |
|
-- 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 |
0 comments:
Post a Comment