Search code examples
c#xmllinqxelementxmlnode

Converting XElement into XmlNode


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.


Solution

  • 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);