Solution to "invalid data at the root level"

Source: Internet
Author: User

It takes a lot of time to search for this issue. My question is the same as this author:

Http://blog.csdn.net/hdzdw/archive/2008/10/26/3152807.aspx

  1. Private void button#click (Object sender, eventargs E)
  2. {
  3. Try
  4. {
  5. Xmldocument Doc = new xmldocument ();
  6. String file = @ "D: \ books. xml ";
  7. File = file. Trim ();
  8. Doc. loadxml (File );
  9. }
  10. Catch (xmlexception ex)
  11. {
  12. MessageBox. Show (ex. Message + "\ r \ n" + ex. stacktrace );
  13. }
  14. }

The premise is that the XML file is qualified and valid. You can see the error "invalid data at the root level" as soon as you run it. I haven't figured it out for a long time. I searched the internet and found a lot of problems. Some netizens say that before loadxml, file = file. Trim (); is used to solve the problem. If you try it, it will still not work. The load () method is used to solve the problem. Check msdn again. The usage of the two methods is different.

Loadxml (): This method reads XML from strings. Loadxml ("<book> <title> C # </title> </book> ");
Load (): The method places the document in the memory and contains an overloaded method that can be used to obtain data from different formats. Load (file)

(This may be because both methods have an overload whose parameter is string, so naturally these two methods will be mixed ).

I hope to help the netizens who are suffering the same mistakes and keep them in mind.

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.