I'm writing a wrapper for a braindead enterprise XML API. I have an XDocument that I need to turn into a string. Due to the fact that their XML parser is so finicky that it cannot even handle whitespace between XML nodes, the document declaration MUST be EXACTLY:
<?xml version="1.0"?>
However, the XDocument.Save() method always adds an encoding attribute in that declaration:
<?xml version="1.0" encoding="utf-16"?>
With the past hour spent on Google and Stack looking for the best way to generate the XML string, the best I can do is:
string result = xmlStringBuilder.ToString().Replace(@"encoding=""utf-16"", string.Empty));
I've tried
xdoc.Declaration = new XDeclaration("1.0", null, null);
and that does succeed at setting the declaration in the XDocument the way I want it; however, when I call the Save() method, the encoding attribute gets magically thrown back in there, no matter what route I go (using TextWriter, adding XmlWriterSettings, etc.).
Does anyone have a better way to do this, or is my code forever doomed to have a paragraph of ranting in comments above the hideous string replace?