954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

problem reading xml file in c#

Hello,

I am trying to read this xml file. But whenever I get to the load part of it, it gives me this error: "The data at the root level is invalid. Line 1, position 1"
This is the code I am working with:

publicstatic Response Message(string doc)
{
		 StringReader reader = new StringReader(doc);
		 XmlTextReader textReader = new XmlTextReader(reader);
		 XmlDocument xmlDoc = new XmlDocument();
		 xmlDoc.Load(textReader); //gets stuck here
return Response.Message(xmlDoc);
}

doc is the filepath of the xml document.

If anybody has any idea on how to solve this problem that would be great.

Dark_Omen
Posting Pro
573 posts since Apr 2004
Reputation Points: 23
Solved Threads: 6
 

try:

publicstatic Response Message(string doc)
{
     XmlTextReader textReader = new XmlTextReader(doc);
     XmlDocument xmlDoc = new XmlDocument(); 
     xmlDoc.Load(textReader); 
     return Response.Message(xmlDoc);
}

If that doesn't work, post the xml document you're trying to read.

Iron_Cross
Junior Poster
117 posts since Jul 2003
Reputation Points: 46
Solved Threads: 2
 

Yeah I fixed tehe problem. The problem was that it had the encoding and version in the xml file, when really I was trying to take the node in a file and put it into an existing xml file, and it was getting messed up because it saw that. So I took it out and it worked.

Thanks for the help though.

Dark_Omen
Posting Pro
573 posts since Apr 2004
Reputation Points: 23
Solved Threads: 6
 

Read data from XML file in C Sharp

To read data from XML file we have to assign XML file to data set using ReadXml() function of dataset.

please check this link related to this topic

http://www.mindstick.com/Articles/851bfc02-9c84-41c4-a3ca-d83b5fd0adf2/

bbauraj
Newbie Poster
6 posts since May 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You