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