The following code produces this output:
You can use the following code as an example
XDocument doc = GetXmlDoc(); using (var stream = new MemoryStream()) { doc.Save(stream, SaveOptions.DisableFormatting); var docBytes = stream.ToArray(); File.WriteAllBytes("fileName.xml", docBytes); }