Convert XmlElement to XElement (Extension Method)

VN:F [1.9.22_1171]
Rating: 1.0/10 (1 vote cast)

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

VN:F [1.9.22_1171]
Rating: 1.0/10 (1 vote cast)
Convert XmlElement to XElement (Extension Method), 1.0 out of 10 based on 1 rating
Convert XmlElement to XElement (Extension Method) Pieter Brinkman avatarAuthor: Pieter Brinkman ()

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+

Convert XmlElement to XElement (Extension Method) WebsiteConvert XmlElement to XElement (Extension Method) TwitterConvert XmlElement to XElement (Extension Method) Google PlusConvert XmlElement to XElement (Extension Method) Linkedin

Related posts

LINQ: Creating a if statement in Linq query
WCF webservice error with DBML objects

5 Responses to Convert XmlElement to XElement (Extension Method)

Comments
Tweets
Pingbacks
  • Kenny Stuart

    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());

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

    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.

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

    Jonathan Gilbert

    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. :-)

    Click on a tab to select how you'd like to leave your comment

    Leave a Reply

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

    *

    You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>