28

I know there is no direct method of doing it but still.. Can we convert XElement element into XmlNode. Options like InnerText and InnerXml are XmlNode specific.

so,if i want to use these options, what can be done to convert XElement into XmlNode and vice versa.

Sangram Nandkhile
  • 17,634
  • 19
  • 82
  • 116
  • There are ways to get InnerXml from XElement - see https://stackoverflow.com/questions/3793/best-way-to-get-innerxml-of-an-xelement – Stuart Mar 22 '11 at 10:41

5 Answers5

63

I use the following extension methods, they seem to be quite common:

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

    public static XmlNode ToXmlNode(this XElement element)
    {
        using (XmlReader xmlReader = element.CreateReader())
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(xmlReader);
            return xmlDoc;
        }
    }
}
BrokenGlass
  • 158,293
  • 28
  • 286
  • 335
  • 3
    I had to make `ToXmlNode` return `xmlDoc.DocumentElement` to get the behavior I expected. In particular, my `XElement` had annotations, but my `XmlNode` did not. Otherwise very helpful. – tallseth Feb 21 '14 at 14:03
  • `return xmlDoc;` should be corrected as `return xmlDoc.FirstChild;` – Ken Kin Sep 04 '17 at 07:01
28

Here is converting from string to XElement to XmlNode and back to XElement. ToString() on XElement is similar to OuterXml on XmlNode.

    XElement xE = XElement.Parse("<Outer><Inner><Data /></Inner></Outer>");

    XmlDocument xD = new XmlDocument();
    xD.LoadXml(xE.ToString());
    XmlNode xN = xD.FirstChild;

    XElement xE2 = XElement.Parse(xN.OuterXml); 
Wes Grant
  • 2,044
  • 16
  • 14
  • You could also use ImportNode https://msdn.microsoft.com/en-us/library/system.xml.xmldocument.importnode(v=vs.110).aspx – seabass Mar 07 '18 at 13:23
3

Based on BrokenGlass's answer, if you wish to embed the XmlNode to an XmlDocument, than use:

public static class MyExtensions
{
    public static XmlNode ToXmlNode(this XElement element, XmlDocument xmlDoc = null)
    {
        using (XmlReader xmlReader = element.CreateReader())
        {
            if(xmlDoc==null) xmlDoc = new XmlDocument();
            return xmlDoc.ReadNode(xmlReader);
        }
    }
}

Note: if you try to insert into a document a node, that is created by a different document, than it will throw an exception: "The node to be inserted is from a different document context."

Community
  • 1
  • 1
jaraics
  • 4,239
  • 3
  • 30
  • 35
3
XElement xelement = GetXElement();
XmlNode node = new XmlDocument().ReadNode(xelement.CreateReader()) as XmlNode;
Mohini Mhetre
  • 912
  • 10
  • 29
2

I think the shortest way is following:

Dim xn as XmlNode = xdoc.ReadNode(xElem.CreateReader)

That's all! Convert to C# is trivial.

Aave
  • 548
  • 1
  • 5
  • 15