1

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.

2 Answers 2

5

Use the XDocument.Parse method.

Sign up to request clarification or add additional context in comments.

2 Comments

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 Really a valuable suggestion
0

Try

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

(Cars will be your root element).

4 Comments

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.