67

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>
p.campbell
  • 98,673
  • 67
  • 256
  • 322
Dean
  • 3,336
  • 6
  • 36
  • 37
  • Does the string have a single root element? – SLaks Sep 13 '10 at 18:15
  • It doesn't necessarily have a root element. – Dean Sep 13 '10 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. – StingyJack Sep 08 '17 at 02:49
  • Found another thread with some more solutions: https://stackoverflow.com/questions/3936056/how-to-create-xelement-from-a-string – Druthipriya KL Oct 19 '20 at 13:32

5 Answers5

108

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.

Helgi
  • 5,428
  • 1
  • 31
  • 48
Aliostad
  • 80,612
  • 21
  • 160
  • 208
  • 1
    Wont this fail if there's no tag at the beginning? If he just has an xml fragment I don't think this will work.. – Jimmy Hoffa Sep 13 '10 at 18:14
  • 2
    @Jimmy Hoffa: IIRC LoadXml takes any well-formed XML fragment that contains exactly one XML element at the top level. ` – dtb Sep 13 '10 at 18:17
  • 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 "" this way would fail. – Suncat2000 Jul 27 '20 at 15:36
27

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>
johnrock
  • 361
  • 4
  • 4
  • 1
    This seems to be the canonically correct way according to Ms docs. I dont know why it isnt the preferred solution – user430788 Dec 24 '19 at 19:28
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;
dtb
  • 213,145
  • 36
  • 401
  • 431
  • 2
    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 Sep 13 '10 at 18:13
  • @Jimmy Hoffa: If you have a document, it's straightforward to get the root element, no? – dtb Sep 13 '10 at 18:14
  • 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.. – Jimmy Hoffa Sep 13 '10 at 18:18
2

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

Here is a simple examle:

XmlDocument xmlDoc = new XmlDocument(); 
xmlDoc.LoadXml("YOUR XML STRING"); 
Florian
  • 538
  • 4
  • 11
0

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

Nandagopal T
  • 2,037
  • 9
  • 42
  • 54