all things Sitecore related

Convert XmlElement to XElement (Extension Method)

By on December 8, 2008 in linq, XML with 5 Comments

I was using some web-services that returned there values in an XmlElement. I needed to convert this XmlElement to something that I can use with LINQ. I didn't find good solutions on the internet. So I started building my own convert logic. After all kinds of solutions I ended up with creating an new XmlDocument, adding the XmlElement to the XmlDocument and then convert the innerXml of the XmlDocument to an XElement. Not really nice but it does the trick.

I've rewritten the code into a Extension Method for the XmlElement.

[code:c#]

public static XElement ToXElement(this XmlElement xml)
{
   XmlDocument doc = new XmlDocument();

   doc.AppendChild(doc.ImportNode(xml, true));

   return XElement.Parse(doc.InnerXml);

}

[/code]


You use the Extension Method like this:

[code:c#]

XmlElement xmlElement = wsClient.DoWebServiceRequest();
XElement xml = xmlElement.ToXElement();

[/code]


Please tell me if you have a better way of doing this!

Cheers,
Pieter

About the Author

About the Author: Pieter is Technical Marketing Manager for Sitecore Netherlands and owner of Gaatverweg.nl. He has more than ten years experience with software developing in multiple programming languages and with different Content Management Systems. Before joining Sitecore Pieter was a lead developer for multiple Sitecore and .Net projects, he joined Sitecore in 2011 as an Solution Architect in The Netherlands, after two years as an Solution Architect he joined the Technical Marketing department. In the role as Techinical Marketing Manager he is responsible for the Global MVP program and the Sitecore technical branding strategy. You can follow Pieter on twitter: @pieterbrink123 or Google+ .

Subscribe

If you enjoyed this article, subscribe now to receive more just like it.

There Are 5 Brilliant Comments

Trackback URL | Comments RSS Feed

  1. Kenny Stuart says:

    Hi Pieter,

    you can request the OuterXml from the XmlElement so no need to create the XmlDocument etc, just use XElement.Parse(xml.OuterXml), also, extension methods are not always the best solution, this is what I use.

    [quote]
    public static XElement MakeElement(XmlNode node, params object[] descendants)
    {
    XElement element = null;
    if(null != node) {
    element = XElement.Parse(node.OuterXml);
    if(null != descendants) element.Add(descendants);
    }
    return element;
    }
    [/quote]

    Then you can do:

    var result = MakeElement(wsClient.DoWebServiceRequest());

  2. Discussion http://forums.asp.net/p/1278169/2437834.aspx#2437834 describes the opposite operation ConvertXNodeToXmlNode

  3. You can request the OuterXml from the XmlElement so no need to create the XmlDocument etc, just use XElement.Parse(xml.OuterXml), also, extension methods are not always the best solution, this is what I use.

  4. Pieter says:

    Hankjmatt > Thanks for the reply, I will try your solution.

  5. Jonathan Gilbert says:

    I haven’t tried this yet, but what about XDocument.ReadFrom(new XmlNodeReader(node))? Seems to me it would be faster than converting to/from a string. :-)

Post a Comment

Your email address will not be published. Required fields are marked *

Top