Search code examples
c#.netxmlstringlinq-to-xml

How to get Xml as string from XDocument?


I am new to LINQ to XML. After you have built XDocument, how do you get the OuterXml of it like you did with XmlDocument?


Solution

  • You only need to use the overridden ToString() method of the object:

    XDocument xmlDoc ...
    string xml = xmlDoc.ToString();
    

    This works with all XObjects, like XElement, etc.