Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

xml upload error :Data at the root level is invalid. Line 1, position 1.

Posted on 2014-11-04
2
Medium Priority
?
3,056 Views
Last Modified: 2014-11-19
I am trying to do a xml upload and i get the The file could not be uploaded. The following error occured: Data at the root level is invalid. Line 1, position 1. error below is my code,

During the doc1.LoadXml(xml) ,the exceptino is thrown. What is the way for me to prevent this and interestingly i gett the string as identical as the original files content and the original file i checked if it is proper xml in a external site and it is a valid xml document, so what am i doing wrong here?

 string filename = Path.GetFileName(FileUpload1.FileName);
                    Stream str = FileUpload1.FileContent;
                    Byte[] pricingdata = new Byte[FileUpload1.FileContent.Length];
                    str.Read(pricingdata, 0, FileUpload1.PostedFile.ContentLength);

                    string xml = Encoding.UTF8.GetString(pricingdata);
                    doc1.LoadXml(xml);
0
Comment
Question by:nicedone
2 Comments
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 2000 total points
ID: 40421462
Your uploaded file probably contains a BOM (byte order marker). There's probably a better way but just to see if the code succeeds when you remove it you can try:
 string filename = Path.GetFileName(FileUpload1.FileName);
                    Stream str = FileUpload1.FileContent;
                    Byte[] pricingdata = new Byte[FileUpload1.FileContent.Length];
                    str.Read(pricingdata, 0, FileUpload1.PostedFile.ContentLength);

                string xml;
                if (string.Format("{0:X2}{1:X2}{2:X2}", pricingdata[0], pricingdata[1], pricingdata[2]) == "EFBBBF") {
                    xml = Encoding.UTF8.GetString(pricingdata, 3, pricingdata.Length - 3);
                } else {
                    xml = Encoding.UTF8.GetString(pricingdata);
                }
                doc1.LoadXml(xml);

Open in new window

When I say there's probably a better way, see for example this old EE solution where it states the stream can detect the encoding. I haven't used this but you could try it instead of what you do now (use UTF-8 hard-coded), although I didn't have a problem with ascii files there could be a problem with unicode files.
0
 

Author Closing Comment

by:nicedone
ID: 40453786
Thanks for the answer ,i totally forgot to rate you although a little late.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Loops Section Overview
Suggested Courses

824 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question