Solved

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

Posted on 2014-11-04
2
2,604 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 500 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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

828 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