16

I'm using C# to output an xml file and Im trying to set the xml encoding value to UTF-8 but its currently outputting:

<?xml version="1.0"?>

This is my code:

public sealed class StringWriterWithEncoding: StringWriter {
    private readonly Encoding encoding;

    public StringWriterWithEncoding(Encoding encoding) {
        this.encoding = encoding;
    }

    public override Encoding Encoding {
        get {
            return encoding;
        }
    }
}

private string GetXml(JobStore jobStore) {
    StringWriterWithEncoding sw = new StringWriterWithEncoding();
    XmlWriterSettings settings = new XmlWriterSettings();
    settings.Encoding = Encoding.UTF8;
    settings.Encoding = Encoding.GetEncoding("utf-8");
    settings.Indent = true;
    using(var writer = XmlWriter.Create(sw, settings)) {
        writer.WriteStartDocument();
        writer.WriteStartElement("resources");

        writer.WriteStartElement("string");
        writer.WriteAttributeString("name");
        writer.WriteCData("value");
        writer.WriteEndElement();

        writer.WriteEndElement();
        writer.WriteEndDocument();
    }
    return sw.ToString();
}

Must be something simple I am missing?

5
  • Can you share your code on .NETFiddle? Commented Mar 3, 2017 at 16:00
  • 2
    Utf8StringWriter from this answer should work. Commented Mar 3, 2017 at 16:17
  • 1
    Your code does not compile -- StringWriterWithEncoding does not have a parameterless constructor. Or, maybe the version you are actually does have a parameterless constructor which leaves encoding at its default (null) value, meaning Encoding returns null? That would explain your problem. Anyway, if I do StringWriterWithEncoding sw = new StringWriterWithEncoding(Encoding.UTF8); everything works correctly. Sample fiddle. Commented Mar 3, 2017 at 16:22
  • Life saver! Thanks a million man! Commented Mar 3, 2017 at 16:29
  • Please do not get into the habit of adding "ASAP" begging to your questions - the volunteers in our community will answer at leisure. Thanks! Commented Mar 4, 2017 at 12:32

2 Answers 2

29

Your code does not compile -- StringWriterWithEncoding does not have a parameterless constructor. Or, if it does have a parameterless constructor, maybe it actually looks like this?

public sealed class StringWriterWithEncoding : StringWriter
{
    private readonly Encoding encoding;

    public StringWriterWithEncoding() { }

    public StringWriterWithEncoding(Encoding encoding)
    {
        this.encoding = encoding;
    }

    public override Encoding Encoding
    {
        get { return encoding; }
    }
}

If so, that would explain your problem - the field encoding has been left at its default (null) value, meaning Encoding returns null, and so no encoding will appear in the XML file.

To fix it, eliminate the parameterless constructor, and do:

var sw = new StringWriterWithEncoding(Encoding.UTF8);

Or change the parameterless constructor to explicitly set Encoding.UTF8:

public sealed class StringWriterWithEncoding : StringWriter
{
    private readonly Encoding encoding;

    public StringWriterWithEncoding() : this(Encoding.UTF8) { }

    public StringWriterWithEncoding(Encoding encoding)
    {
        this.encoding = encoding;
    }

    public override Encoding Encoding
    {
        get { return encoding; }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

-4

Just use the output file name for the XmlWriter instead of the StringWriterWithEncoding:

var settings = new XmlWriterSettings
{
    Encoding = Encoding.UTF8,
    Indent = true
};

using (var writer = XmlWriter.Create(filePathName, settings))
{
    ...

3 Comments

Even this this, the XML header is still <?xml version="1.0" encoding="utf-16"?>
This does not work. XML header encoding still utf-16
Doesn't work because the encoding is overwritten by the encoding set on the stringwriter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.