XDocument to string: how to skip encoding in an ad?

I am writing a wrapper for Braindead's XML API. I have an XDocument that I need to turn into a string. Because their XML parser is so sophisticated that it cannot even handle spaces between XML nodes, the document declaration MUST BE EXACT:

<?xml version="1.0"?>

However, the XDocument.Save () method always adds an encoding attribute to this declaration:

<?xml version="1.0" encoding="utf-16"?>

With the last hour spent on Google and Stack looking for a better way to generate an XML string, I can do this:

string result = xmlStringBuilder.ToString().Replace(@"encoding=""utf-16"", string.Empty));

I tried

xdoc.Declaration = new XDeclaration("1.0", null, null);

XDocument , ; , Save(), , , ( TextWriter, XmlWriterSettings ..).

- , ?

+5
1

, , XML, -, XML, .NET, XML, , :

public class MyStringWriter : StringWriter
{
    public override Encoding Encoding
    {
        get
        {
            return null;
        }
    }
}

    XDocument doc = new XDocument(
        new XDeclaration("1.0", null, null),
        new XElement("root", "test")
        );

    string xml;

    using (StringWriter msw = new MyStringWriter())
    {
        doc.Save(msw);
        xml = msw.ToString();
    }

    Console.WriteLine(xml);

<?xml version="1.0"?>
<root>test</root>
+10

All Articles