Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2014-11-04
2
Medium Priority
?
2,889 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

719 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