Solved

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

Posted on 2014-11-04
2
2,280 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
Comment Utility
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
Comment Utility
Thanks for the answer ,i totally forgot to rate you although a little late.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now