I need to convert an XML string into an XmlElement
Asked Answered
N

5

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>
Nappy answered 13/9, 2010 at 18:6 Comment(4)
Does the string have a single root element?Scheld
It doesn't necessarily have a root element.Nappy
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.Brabazon
Found another thread with some more solutions: #3936556Judicial
P
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.

Plant answered 13/9, 2010 at 18:11 Comment(3)
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..Byronbyrum
@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.Invective
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.Cooperage
S
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>
Sills answered 14/11, 2013 at 3:34 Comment(1)
This seems to be the canonically correct way according to Ms docs. I dont know why it isnt the preferred solutionKincardine
I
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;
Invective answered 13/9, 2010 at 18:10 Comment(3)
He wanted the element, and for XElement he can just do XElement.Parse(xmlString), but you're giving him a document not element.Byronbyrum
@Jimmy Hoffa: If you have a document, it's straightforward to get the root element, no?Invective
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..Byronbyrum
D
2

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

Here is a simple examle:

XmlDocument xmlDoc = new XmlDocument(); 
xmlDoc.LoadXml("YOUR XML STRING"); 
Dewberry answered 13/9, 2010 at 18:14 Comment(0)
P
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

Phthisis answered 12/5, 2012 at 5:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.