Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET XML VB.NET
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 23-May-11 3:59am
Edited 23-May-11 5:09am
v5
Comments
SAKryukov at 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 at 23-May-11 13:07pm
   
Exactly! I asked him the same in my answer. OP expects us to read his/her mind.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
Ravi Sharma 2 at 23-May-11 9:22am
   
I do above 2 point, but still i got same error.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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?
  Permalink  
v5
Comments
Ravi Sharma 2 at 23-May-11 10:08am
   
This is not solved the problem
Tarun.K.S at 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".
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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 Smile | :)
 
Hope this will help you Smile | :)
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

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);
  Permalink  
v2

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

  Print Answers RSS
0 OriginalGriff 304
1 Sergey Alexandrovich Kryukov 255
2 Shweta N Mishra 216
3 Maciej Los 210
4 PIEBALDconsult 184
0 OriginalGriff 7,660
1 Sergey Alexandrovich Kryukov 7,072
2 DamithSL 5,586
3 Manas Bhardwaj 4,946
4 Maciej Los 4,665


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 25 May 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100