Click here to Skip to main content
15,795,331 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:

I am having .net application and I have an xml document.I want to get the values of childnodes and put it into notepad.After I run the program the error is occurs.
"Data at the root level is invalid. Line 1, position 1.".Plz help me.Below is the code

XmlDocument xml = new XmlDocument();
       xml.LoadXml("C:/Documents and Settings/sridharan/Desktop/RTSLINK FOR TALLY/XMLFile3.xml");
       XmlNodeList xnlist = xml.SelectNodes("/Names/Name");
       foreach (XmlNode xn in xnlist)
           string firstname = xn["firstname"].InnerText;
           string lastname = xn["secondname"].InnerText;
           Console.WriteLine("Name:{0} {1}", firstname, lastname);

Sergey Alexandrovich Kryukov 18-May-11 1:20am    
Everything is hard-coded. Very bad code.
sridharan28 18-May-11 1:27am    
thanks for ur reply
sridharan28 18-May-11 1:25am    
Thanks alot for your reply.Please give me suitable answer
Sergey Alexandrovich Kryukov 18-May-11 1:49am    
I commented to the answer by S Mewara. I don't see the problem you report from your code. Are you sure this is the same code as the one which cause the problem? Perhaps you did not properly reported a problem. Check it up. Catch all exceptions around the code which throws it and dump exception stack, don't forget all inner exceptions (recursively); show the code lines in code file.
Sergey Alexandrovich Kryukov 18-May-11 2:03am    
OK, you got a real answer. I suggest you do it right. It's easy, too. Please see my solution.

Similar issue discussed[^] here at length.

Few possible reasons are:
1. Not a well formed XML
2. Having a white space just before the root element
3. Wrong reference of the file
4. Encoding specified in XML document

Have a look at this discussion[^] too.

Apart from above answer, try two things:
1. Use xml.Load instead of xml.LoadXml method.
2. If 1 does not do, put this as the first line (make sure no space before the first character)
<?xml version="1.0" encoding="utf-8"?>
Share this answer
Sergey Alexandrovich Kryukov 18-May-11 1:23am    
I'm not sure. First, items 1, 2 and 4 is the same as 1. Secondly, you see the XML code, it looks like well-formed. Item 3 is not applicable (see the error message).
Most probably, OP did not report the problem correctly...
Sandeep Mewara 18-May-11 1:46am    
In a sense, agree 1,2 & 4 sounds same. But 1 is just for opening/closing tags. :)

Based on the thread references, I see that 3 can be the reason... though I totally agree again. To me, point 2 or 4 has highest chance.

Meanwhile, since you posted these comments, let me read more on what and why of this error - if I get anything else, I will share with you.
Sergey Alexandrovich Kryukov 18-May-11 2:49am    
No! 1 is not just about tags. There is a whole lot of well-formed rules. See my comment below. Why doing all that?
sridharan28 18-May-11 1:26am    
Exactly i am getting the error "Data at the root level is invalid. Line 1, position 1."
Sandeep Mewara 18-May-11 1:51am    
Can you try putting this as the first line:
<?xml version="1.0" encoding="utf-8" ?>
Here is the alternative. I would totally avoid this approach. I don't see why you need to deal with XML manually. Use Data Contract. It will robustly save your data model to XML and restore it back; the schema will be created on the fly. This is the most non-intrusive method as well: you only add attributes [DataContract] and [DataMembers].


Share this answer
Here's what your problem is:

XmlDocument has 2 load methods for loading XML

XmlDocument xml = new XmlDocument();

the passed paramater here should be a valid xml string.

the method you want is

XmlDocument xml = new XmlDocument();

Also, the XPath is case sensitive (Names is different than name)
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900