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
-
- Private void button#click (Object sender, eventargs E)
-
- {
-
- Try
-
- {
-
- Xmldocument Doc = new xmldocument ();
-
- String file = @ "D: \ books. xml ";
-
-
- File = file. Trim ();
-
- Doc. loadxml (File );
-
-
- }
- Catch (xmlexception ex)
-
- {
-
- MessageBox. Show (ex. Message + "\ r \ n" + ex. stacktrace );
-
- }
-
- }
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.