Click here to Skip to main content
15,905,233 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Dear Developer I store the XML output to String and Again convert this string to XML .I successfully convert XML output to String, but i got problem again converting string to XML.
I share my code

webservice.Service1 objService1 = new webservice.Service1();
       String s = objService1.HelloWorld();   //Convert XML output into String
       XmlDocument xd = new XmlDocument();
       xd.LoadXML(s);


I use LoadXML() method, but i got error "Data at the root level is invalid. Line 1 position 1." .Its greatful, if any body give right code to convert String To XML in c#.

Thanks In advance
Regards,
Ravi
Posted
Updated 23-May-11 4:09am
v5
Comments
Sergey Alexandrovich Kryukov 23-May-11 12:14pm    
So, what do you have in this line of XML? How can you get help without showing it?
--SA
Tarun.K.S 23-May-11 13:07pm    
Exactly! I asked him the same in my answer. OP expects us to read his/her mind.

The piece of code looks ok to me. I think the problem is with your xml data.
A few suggestions:

1.) Can you confirm that the XML is well formed? Have you tried to open the XML file using IE and ensuring that there are no errors?
2.) Make sure there is no blank space before the root element.
 
Share this answer
 
Comments
Ravi Sharma 2 23-May-11 9:22am    
I do above 2 point, but still i got same error.
One think I have noticed is the variable mismatch. You have XML to string in "s" variable while you are passing "s1" to the LoadXML() method.

You can ensure the XML generation from string (i.e. from variable "s" in this case), by simply saving the output of "s" in a XML file.

Hope it helps.
 
Share this answer
 
Considering Manav's points, you must have missed the XML Declaration.

try this:
string s = "<?xml version = \"1.0\"?>" + objServicel.HelloWorld();

Now load : xd.LoadXml(s);

Did it work now?
 
Share this answer
 
v5
Comments
Ravi Sharma 2 23-May-11 10:08am    
This is not solved the problem
Tarun.K.S 23-May-11 10:20am    
Ok well can you tell what is the result that you get in String s = objService1.HelloWorld();?
Give me the value of "s".
Member 14564587 24-Oct-19 2:59am    
tell me, What is this "objService1.HelloWorld();?"
Hi,

Well all the provided solutions are correct. But for efficient Conversion and use.

You should use XDocument. XDocument is better than XMLDocument. It is very efficient, simple and easy to use.

Your code :

webservice.Service1 objService1 = new webservice.Service1();
        String s = objService1.HelloWorld();   //Convert XML output into String   
        XmlDocument xd = new XmlDocument();
        xd.LoadXML(s);


Solution:

XDocument xd = XDocument.Parse(s);


Thanks :)

Hope this will help you :)
 
Share this answer
 
v2
I think the the problem is because your input XML String is missing root element of the XML document. You can add it programatically using the following code..

XmlDocument doc = new XmlDocument();
doc.LoadXml(inputxmlstring);

//Create an XML declaration.
XmlDeclaration xmldecl;
xmldecl = doc.CreateXmlDeclaration("1.0",null,null);

//Add the new node to the document.
XmlElement root = doc.DocumentElement;
doc.InsertBefore(xmldecl, root);
 
Share this answer
 
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