Converting String object to XDocument object

K

K Viltersten

I have a method as follows.

private String GetXMLString(){...}

It produces a string corresponding to
contents of a valid XML-document. Now,
my problem is that the class XDocument
seems not to have a constructor with
String as a parameter.

How can i easily trasform my String
into XDocument?
 
A

Alberto Poblacion

K Viltersten said:
I have a method as follows.

private String GetXMLString(){...}

It produces a string corresponding to contents of a valid XML-document.
Now, my problem is that the class XDocument
seems not to have a constructor with
String as a parameter.

How can i easily trasform my String into XDocument?

You can use the method Load() to load the XDocument from a variety of
sources. One of the overloads takes a TextReader, which you can feed with a
StringReader (which derives from TextReader) connected to your String:

XDocument xd = new XDocument();
xd.Load(new StringReader(GetXMLString()));
 
M

Martin Honnen

K said:
I have a method as follows.

private String GetXMLString(){...}

It produces a string corresponding to contents of a valid XML-document.
Now, my problem is that the class XDocument
seems not to have a constructor with
String as a parameter.

How can i easily trasform my String into XDocument?

Use the static Parse method:
XDocument doc = XDocument.Parse(GetXMLString());
 
K

K Viltersten

Oh, like that! All right. I also found

XDocument.Parse(String)

when i realized that i'm supposed to
use the XDocument class' statics!

Thanks!

--
Regards
K Viltersten
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top