The data at the root level is invalid. Row 1, position 1

Source: Internet
Author: User
Public void parse (string XML) {This. items = new rssitemcollection (); xmldocument xdoc = new xmldocument (); try {// xdoc. loadxml (XML); xdoc. loadxml (XML. trim ();} catch (exception e) {Throw new exception ("the content does not conform to the standard", e );}}
View code

During XML loading, the data at the root level is always invalid. Row 1, position 1

I have not found any results on the Internet for a long time. I only tried it myself. I added xml = xml before loading. trim (); passed, but there are no strange errors such as spaces before and after the XML, and the loaded XML is not reported in IE.

Dare not share it with others who share the same experience

From: http://hi.baidu.com/xsharkx/item/d2da2935cb6995f6a88428cc

 

Personal solution:

When a previously used XML file is loaded with only a few characters, "the root-level data is invalid. Although the above solution has been found and the problem has been solved

But it's not clear why?

Do strings that look exactly the same ??

Copy the following code to the chrome console and run it.

1 var s=‘<?xml version="1.0" encoding="UTF-8"?>‘;2 var s=‘<?xml version="1.0" encoding="UTF-8"?>‘;3 4 alert(‘s.length=‘+s.length+‘\r\n_s.length=‘+_s.length);
View code running result: Debugging in vs shows that there is one more character without trim ". "causes XML errors. Solution: 1. add trim () to the Code to clear unnecessary characters (recommended)
xDoc.LoadXml(xml.Trim());
2. automatically clear unnecessary characters "." and replace it in the XML file (this method does not completely avoid this issue should be used in method 1) http://hi.baidu.com/xsharkx/item/d2da2935cb6995f6a88428cc http://www.cnblogs.com/XChWaad/p/3346875.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.