Quantcast
Viewing all articles
Browse latest Browse all 34

Convert XElement to XmlNode (and Convert XmlNode to XElement)

[Blog Map]  This blog is inactive.  New blog: EricWhite.com/blog

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.

(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 = newXElement("QueryOptions",
    newXElement("Folder", "Open XML Documents"),
    newXElement("ViewAttributes", newXAttribute("Scope", "Recursive"))
);

XElement viewFields = newXElement("ViewFields",
    newXElement("FieldRef", newXAttribute("Name", "GUID")),
    newXElement("FieldRef", newXAttribute("Name", "ContentType")),
    newXElement("FieldRef", newXAttribute("Name", "BaseName")),
    newXElement("FieldRef", newXAttribute("Name", "Modified")),
    newXElement("FieldRef", newXAttribute("Name", "EncodedAbsUrl"))
);

XElement listContent = wsList
    .GetListItems(documentLibraryGUID,
                  null, null, viewFields.GetXmlNode(),
                  null, queryOptions.GetXmlNode(), webId)
    .GetXElement();

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;

publicstaticclassMyExtensions
{
    publicstaticXElement GetXElement(thisXmlNode node)
    {
        XDocument xDoc = newXDocument();
        using (XmlWriter xmlWriter = xDoc.CreateWriter())
            node.WriteTo(xmlWriter);
        return xDoc.Root;
    }

    publicstaticXmlNode GetXmlNode(thisXElement element)
    {
        using (XmlReader xmlReader = element.CreateReader())
        {
            XmlDocument xmlDoc = newXmlDocument();
            xmlDoc.Load(xmlReader);
            return xmlDoc;
        }
    }
}

classProgram
{
    staticvoid Main(string[] args)
    {
        XElement e = newXElement("Root",
            newXElement("Child",
                newXAttribute("Att", "1")
            )
        );

        XmlNode xmlNode = e.GetXmlNode();
        Console.WriteLine(xmlNode.OuterXml);

        XElement newElement = xmlNode.GetXElement();
        Console.WriteLine(newElement);
    }
}

Code is attached.

Image may be NSFW.
Clik here to view.

Viewing all articles
Browse latest Browse all 34

Trending Articles