Got it! C#'s XDocument.ToString() drops the <?xml?>
directive. My other query worked because I was using XmlDocument.InnerXml, which didn't do that (but looked uglier compared to XDocument). I went ahead and just concatenated:
doc.Declaration.ToString() + doc.ToString()
Basically the same problem as XDocument.ToString() drops XML Encoding Tag