tags:

views:

58

answers:

2

How to convert the following string to xml ?

 var query = @"<Cars><Car manufacturer='lindo'/>
               <Car manufacturer='Opera'/></Cars>";

As it is a string i can't apply the extension method .cast() to convert it to XDocument or XElement.

+4  A: 

Use the XDocument.Parse method.

itowlson
Oops what a flash reply
Dina
When in doubt, if you have a `string` and you want to make a `Something` from it, look for a method named `Something.Parse`.
Jason
@Jason Really a valuable suggestion
Dina
A: 

Try

 var queryXml = "<?xml version=\"1.0\"?>" + query;
 var xdoc = new XmlDocument();
 xdoc.LoadXml(queryXml);

(Cars will be your root element).

Doc Brown
why the closing tag </xml> is not allowed when i put "<?xml..> <Cars><Car>....</Car> </xml>"
Dina
@Dina because (a) there is no "opening tag" and (b) "xml" in any capitalization is reserved in XML
peter.murray.rust
@Dina: the ?xml is not the opening tag for the xml document. It is like a header. You need to have <?xml...><xml>...</xml>
John Buchanan
@Dina you actually need `</Cars>`
peter.murray.rust
@ALL thanks to all:)
Dina