Convert XElement to XmlNode (and Convert XmlNode to XElement)

Sometimes you want to convert an XmlNode to an XElement and back again.  Some programming libraries define methods that take XmlNode objects as parameters.  These libraries also may contain properties and methods that return XmlNode objects.  However, it is more convenient to work with LINQ to XML instead of the classes in System.Xml (XmlDocument, XmlNode, etc.)  This post presents a bit of code to do these conversions.

This blog is inactive.
New blog:

Blog TOC(Update March 5, 2009 - I've written a blog post that shows how to convert from XDocument to XmlDocument (and vice versa).  The code presented in that post is a superset of the code presented in this code.)

As an example of where you need these methods, you can do a lot with SharePoint using web services.  The proxy classes that wsdl.exe creates contain methods that use classes in System.Xml.

It is convenient to write these conversions as extension methods.  When converting to and from classes in System.Xml, the code reads much better when you tack an extension method on the end instead of surrounding the expression with a method call.  The following code shows how to call the GetListItems method of the Lists web service using the GetXmlNode and GetXElement extension methods:

XElement queryOptions = new XElement("QueryOptions",
    new XElement("Folder", "Open XML Documents"),
    new XElement("ViewAttributes", new XAttribute("Scope", "Recursive"))

XElement viewFields = new XElement("ViewFields",
    new XElement("FieldRef", new XAttribute("Name", "GUID")),
    new XElement("FieldRef", new XAttribute("Name", "ContentType")),
    new XElement("FieldRef", new XAttribute("Name", "BaseName")),
    new XElement("FieldRef", new XAttribute("Name", "Modified")),
    new XElement("FieldRef", new XAttribute("Name", "EncodedAbsUrl"))

XElement listContent = wsList
                  null, null, viewFields.GetXmlNode(),
                  null, queryOptions.GetXmlNode(), webId)

To create an XmlNode from an XElement, you create an XmlReader using the XNode.CreateReader method, create an XmlDocument, and load the document using the XmlReader.  XmlDocument inherits XmlNode.

To create an XElement from an XmlNode, you create an XDocument, create an XmlWriter using the XContainer.CreateWriter method, write the XmlNode to the XDocument, and return the root element of the XDocument.

The following example contains the extension methods and some code that shows their use:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using System.Xml.Linq;

public static class MyExtensions
    public static XElement GetXElement(this XmlNode node)
        XDocument xDoc = new XDocument();
        using (XmlWriter xmlWriter = xDoc.CreateWriter())
        return xDoc.Root;

    public static XmlNode GetXmlNode(this XElement element)
        using (XmlReader xmlReader = element.CreateReader())
            XmlDocument xmlDoc = new XmlDocument();
            return xmlDoc;

class Program
    static void Main(string[] args)
        XElement e = new XElement("Root",
            new XElement("Child",
                new XAttribute("Att", "1")

        XmlNode xmlNode = e.GetXmlNode();

        XElement newElement = xmlNode.GetXElement();

Code is attached.


Comments (11)

  1. [Blog Map] Web services are one of the most effective and convenient ways for casual developers to access

  2. Jaideep says:

    A very good article indeed. can you let me know how to convert a XDocument or XElement to XmlDocument?


  3. SharePoint WebService are your friend Ever wanted access to that treasure trove of information available

  4. This is just way cool.  Check out Eric White’s blog for a slick use of extension methods to make

  5. Here I was thinking that I would need to use your code because of this:


    public List<XmlElement> UnknownElements { get; set; }

    Where in fact it works fine with:


    public List<XElement> UnknownElements { get; set; }

    In any case thanks for the helpful post.

  6. sing says:

    this article is what I need now thanks –a chinese boy

  7. Keith Townssend says:

    I had the same scenario as Jonathan Dickinson (in comments) and I thought I needed your great tip, but as he points out [XmlAnyElement] works fine on a property of List<XElement> even though documentation I found says it requries a property of XmlElement[].

    I still needed to do manual conversion between XAttribute and XmlAttribute because the lack of a paramaterless constructor on XAttribute means [XmlAnyAttribute] won't work on property of List<XAttribute>. Still, your code is good and it got me where I needed to be. Thanks!

  8. ktwis says:

    nice post thanks

  9. Pierce Yu says:

    the function GetXmlNode() does not work well.

    in order to convert an XElement to XmlNode, the function GetXmlNode() needs to be updated as below:

          public static XmlNode GetXmlNode(XElement element)


               using (XmlReader xmlReader = element.CreateReader())


                   XmlDocument xmlDoc = new XmlDocument();

                   return xmlDoc.ReadNode(xmlReader);



Skip to main content