public static void Main()
string xml = @"<?xml version='1.0' encoding='utf-8'?>
XDocument xDoc = XDocument.Parse(xml);
Console.WriteLine(xDoc.ToStringWithDeclaration());
public static class XDocumentExtensions
public static string ToStringWithDeclaration(this XDocument doc)
throw new ArgumentNullException("doc");
StringBuilder builder = new StringBuilder();
using (TextWriter writer = new StringWriter(builder))
return builder.ToString();