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?
Well the receiving end should be fixed to use an XML parser and not something that breaks with XML syntax but with .NET if you want to create a string with the XML declaration as you posted it the following approach works for me:
and then
outputs