Created
          July 20, 2012 12:11 
        
      - 
      
- 
        Save rarous/3150395 to your computer and use it in GitHub Desktop. 
    Convert XElement to XmlElement
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | using System.Xml; | |
| using System.Xml.Linq; | |
| public static class XElementExtensions | |
| { | |
| public static XmlElement ToXmlElement(this XElement el) | |
| { | |
| var doc = new XmlDocument(); | |
| doc.Load(el.CreateReader()); | |
| return doc.DocumentElement; | |
| } | |
| } | 
Nice; found this while working in a legacy code area. For converting to the middle of an existing XmlDocument instance, you can make this a bit messier like:
public static XmlElement ToXmlElement(this XElement el, XmlDocument ownerDocument)
{
    return (XmlElement)ownerDocument.ReadNode(el.CreateReader());
}
The returned element could then be appended as a child of any element in ownerDocument.
π π π π π
Thanks a lot!! It's very helpful.
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
            
Very helpful. You may want to wrap el.CreateReader() in a using statement as it returns an object that implements IDisposable.