69

I'm looking for the simplest way to convert a string containing valid XML into an XmlElement object in C#.

How can you turn this into an XmlElement?

<item><name>wrench</name></item> 
4
  • Does the string have a single root element? Commented Sep 13, 2010 at 18:15
  • It doesn't necessarily have a root element. Commented Sep 13, 2010 at 18:22
  • 1
    The way this is handled in .NET today is still frustratingly dumb. When the WSDL you dont control wants XmlElement[] after the svcutil generates your proxy, you are kind of forced weirdness. Commented Sep 8, 2017 at 2:49
  • Found another thread with some more solutions: stackoverflow.com/questions/3936056/… Commented Oct 19, 2020 at 13:32

5 Answers 5

109

Use this:

private static XmlElement GetElement(string xml) { XmlDocument doc = new XmlDocument(); doc.LoadXml(xml); return doc.DocumentElement; } 

Beware!! If you need to add this element to another document first you need to Import it using ImportNode.

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

3 Comments

Wont this fail if there's no <?xml version bla bla> tag at the beginning? If he just has an xml fragment I don't think this will work..
@Jimmy Hoffa: IIRC LoadXml takes any well-formed XML fragment that contains exactly one XML element at the top level. <?xml at the beginning is not required.
To be clear, this works only if the XML fragment does not contain more than a single element, so it can be treated as a root element. Otherwise, it throws an XmlException stating, "There are multiple root elements." For example, loading "<item/><item/>" this way would fail.
29

Suppose you already had a XmlDocument with children nodes, And you need add more child element from string.

XmlDocument xmlDoc = new XmlDocument(); // Add some child nodes manipulation in earlier // .. // Add more child nodes to existing XmlDocument from xml string string strXml = @"<item><name>wrench</name></item> <item><name>screwdriver</name></item>"; XmlDocumentFragment xmlDocFragment = xmlDoc.CreateDocumentFragment(); xmlDocFragment.InnerXml = strXml; xmlDoc.SelectSingleNode("root").AppendChild(xmlDocFragment); 

The Result:

<root> <item><name>this is earlier manipulation</name> <item><name>wrench</name></item> <item><name>screwdriver</name> </root> 

1 Comment

This seems to be the canonically correct way according to Ms docs. I dont know why it isnt the preferred solution
15

Use XmlDocument.LoadXml:

XmlDocument doc = new XmlDocument(); doc.LoadXml("<item><name>wrench</name></item>"); XmlElement root = doc.DocumentElement; 

(Or in case you're talking about XElement, use XDocument.Parse:)

XDocument doc = XDocument.Parse("<item><name>wrench</name></item>"); XElement root = doc.Root; 

3 Comments

He wanted the element, and for XElement he can just do XElement.Parse(xmlString), but you're giving him a document not element.
@Jimmy Hoffa: If you have a document, it's straightforward to get the root element, no?
Sure, was just saying your answer could be tailored to the posters question a little more in case it's not as easy for him as it is for us..
2

You can use XmlDocument.LoadXml() to do this.

Here is a simple examle:

XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml("YOUR XML STRING"); 

Comments

1

I tried with this snippet, Got the solution.

// Sample string in the XML format String s = "<Result> No Records found !<Result/>"; // Create the instance of XmlDocument XmlDocument doc = new XmlDocument(); // Loads the XML from the string doc.LoadXml(s); // Returns the XMLElement of the loaded XML String XmlElement xe = doc.DocumentElement; // Print the xe Console.out.println("Result :" + xe); 

If any other better/ efficient way to implement the same, please let us know.

Thanks & Cheers

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.