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?
Just use the output file name for the XmlWriter instead of the StringWriterWithEncoding:
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?If so, that would explain your problem - the field
encoding
has been left at its default (null) value, meaningEncoding
returns null, and so no encoding will appear in the XML file.To fix it, eliminate the parameterless constructor, and do:
Or change the parameterless constructor to explicitly set
Encoding.UTF8
: