Converting String object to XDocument object

  • Thread starter Thread starter K Viltersten
  • Start date Start date
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?
 
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()));
 
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());
 
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
 
Back
Top