How to Convert XmlDocument to XDocument in C# ?

Published on February 7, 2016 by abundantcode

Filed under XML

Last modified February 6, 2016

Print this page

rate 1 star rate 2 star rate 3 star rate 4 star rate 5 star
Your rating: none, Average: 0 (0 votes)

This article have been viewed 2095 times

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();
            var xDocument = ConvertToXDocument(xmlDocumentobj);


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

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

Leave a Comment


You might be Interested in these Posts

How to Convert a string in JSON.NET to XML in C# ?

You can use the JsonConvert.DeserializeXmlNode method of JSON.NET to convert from the json string to...

How to populate XDocument from a String in C# ?

Want to load or populate XDocument from a string in C# ? . Use the Parse method defined in the XDocu...

How to Populate XDocument from String in C# ?

To populate the XDocument from a string in C# , we can use the Parse method defined in the XDocument...