Solved

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

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

803 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