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


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

//Default.aspx.cs//
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);


//xmlfile//
<names>
  <name>
    <firstname>John</firstname>
    <lastname>Smith</lastname>
  </name>
  <name>
    <firstname>James</firstname>
    <lastname>White</lastname>
  </name>
</names>
Posted
Comments
   
Everything is hard-coded. Very bad code.
--SA
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
   
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.
--SA
   
OK, you got a real answer. I suggest you do it right. It's easy, too. Please see my solution.
--SA

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.

UPDATE:
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
<?xml version="1.0" encoding="utf-8"?>
   
v2
Comments
   
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...
--SA
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.
   
No! 1 is not just about tags. There is a whole lot of well-formed rules. See my comment below. Why doing all that?
--SA
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" ?>
   
Parser can work without it if I'm not much mistaken... good to check up... even if so, it wouldn't hurt, of course.
--SA
   
Sandeep, you know what? I see no point for OP to ram this problem.
Instead of using this naive home-baked approach to serialization, why not using ready-to-use technology which will serialize and deserialize everything automatically?

Please see my answer.
--SA
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].

See:
http://msdn.microsoft.com/en-us/library/ms733127.aspx[^],
http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractserializer.aspx[^],
http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractattribute.aspx[^],
http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datamemberattribute.aspx[^].

—SA
   
Here's what your problem is:

XmlDocument has 2 load methods for loading XML

XmlDocument xml = new XmlDocument();
xml.LoadXml(...)


the passed paramater here should be a valid xml string.

the method you want is

XmlDocument xml = new XmlDocument();
xml.Load("PathGoesHere.xml")


Also, the XPath is case sensitive (Names is different than name)
   
v2

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