How to Convert XmlDocument to XDocument in C# ?

If you want to convert an XmlDocument to XDocument in C# , you can use the XmlNodeReader which does the conversion for you. Use the MoveToContent method and load it to the XDocument class.

How to Convert XmlDocument to XDocument in C# ?

using System;
using System.Xml;
using System.Xml.Linq;

namespace ACCode
{
    class Program
    {
        static void Main(string[] args)
        {
            var xmlDocumentobj = new XmlDocument();
            xmlDocumentobj.LoadXml("<Root><User1>Test</User1></Root>");
            var xDocument = ConvertToXDocument(xmlDocumentobj);

            Console.ReadLine();
        }

        // Method to convert the XmlDocument to XDocument
        public static XDocument ConvertToXDocument( XmlDocument input)
        {
            using (var reader = new XmlNodeReader(input))
            {
                reader.MoveToContent();
                return XDocument.Load(reader);
            }
        }

    } 
}