1

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.

sample 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 grateful, if any body give right code to convert String To XML in c#. Thank you,

2
  • 1
    Please show value of s - there is a good chance that it is not an XML. Commented May 20, 2013 at 1:26
  • Put the string response from the HelloWorld() method. Commented May 20, 2013 at 8:41

2 Answers 2

7

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);
Sign up to request clarification or add additional context in comments.

1 Comment

It is unclear why using different XML parser somehow will make some text valid XML (if another parser could not recognize it). Also "thank you notes" are not necessary...
2
      XmlDocument xd = new XmlDocument();
      xd.LoadXml("<root>123</root>");

It works. You should print the s value and check it is a valid xml string.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.