Problem
It’s a really basic issue that I’m having. To create an XML file, I use XDocument. Then I’d like to return it as an XmlDocument object. I also have an XmlDocument variable that I need to convert to XDocument so that I can attach more nodes.
So, how do you convert XML between XDocument and XmlDocument in the most effective way? (Without employing any file-based temporary storage.)
Asked by Wim ten Brink
Solution #1
The built-in xDocument can be used. To convert back and forth, use CreateReader() and an XmlNodeReader.
To make it easier to deal with, I’m putting it into an Extension method.
using System;
using System.Xml;
using System.Xml.Linq;
namespace MyTest
{
internal class Program
{
private static void Main(string[] args)
{
var xmlDocument = new XmlDocument();
xmlDocument.LoadXml("<Root><Child>Test</Child></Root>");
var xDocument = xmlDocument.ToXDocument();
var newXmlDocument = xDocument.ToXmlDocument();
Console.ReadLine();
}
}
public static class DocumentExtensions
{
public static XmlDocument ToXmlDocument(this XDocument xDocument)
{
var xmlDocument = new XmlDocument();
using(var xmlReader = xDocument.CreateReader())
{
xmlDocument.Load(xmlReader);
}
return xmlDocument;
}
public static XDocument ToXDocument(this XmlDocument xmlDocument)
{
using (var nodeReader = new XmlNodeReader(xmlDocument))
{
nodeReader.MoveToContent();
return XDocument.Load(nodeReader);
}
}
}
}
Sources:
Answered by Mark Coleman
Solution #2
This single-line solution works nicely for me.
XDocument y = XDocument.Parse(pXmldoc.OuterXml); // where pXmldoc is of type XMLDocument
Answered by Abhi
Solution #3
If you need to convert a System.Xml.Linq.XDocument instance to a System.Xml.XmlDocument instance, this extension method will ensure that the XML declaration is preserved in the resulting XmlDocument instance:
using System.Xml;
using System.Xml.Linq;
namespace www.dimaka.com
{
internal static class LinqHelper
{
public static XmlDocument ToXmlDocument(this XDocument xDocument)
{
var xmlDocument = new XmlDocument();
using (var reader = xDocument.CreateReader())
{
xmlDocument.Load(reader);
}
var xDeclaration = xDocument.Declaration;
if (xDeclaration != null)
{
var xmlDeclaration = xmlDocument.CreateXmlDeclaration(
xDeclaration.Version,
xDeclaration.Encoding,
xDeclaration.Standalone);
xmlDocument.InsertBefore(xmlDeclaration, xmlDocument.FirstChild);
}
return xmlDocument;
}
}
}
Hope that helps!
Answered by Dmitry Pavlov
Solution #4
For an XmlDocument, you may try writing the XDocument to an XmlWriter piped to an XmlReader.
A direct conversion is not possible if I understand the principles well (the internal structure of XDocument is different / simplified). But then again, I could be incorrect…
Answered by Daren Thomas
Solution #5
On http://blogs.msdn.com/marcelolr/archive/2009/03/13/fast-way-to-convert-xmldocument-into-xdocument.aspx, there is a discussion.
The fastest technique appears to be reading an XDocument with an XmlNodeReader. More information can be found on the blog.
Answered by paul
Post is based on https://stackoverflow.com/questions/1508572/converting-xdocument-to-xmldocument-and-vice-versa