I know that there is a lot of tutorials about this and even answered questions here, but I have problem I'm trying to resolve for hours and I read almost everything here, but this still remains mistery for me. Please help:
I'm creating XML, and it's created, but the problem is that encoding is UTF-16, and it should be UTF-8. This is what I tried so far, but still is UTF-16:
var xmlText = new StringBuilder();
using (var xml = XmlWriter.Create(xmlText))
{
xml.WriteStartDocument();
xml.WriteStartElement("Weather");
if (model.ModuleList[0] != null)
{
foreach (var weather in model.ModuleList)
{
var AddProperty = new Action<XmlWriter, ModuleModel>((a, forc) =>
{
xml.WriteStartElement("Forecast");
a.WriteElementString("Description", forc.Description);
a.WriteElementString("Date", forc.Date.ToString());
a.WriteElementString("MinTemp", forc.Min_Temp.ToString());
a.WriteElementString("MaxTemp", forc.Max_Temp.ToString());
a.WriteElementString("Pressure", forc.Pressure.ToString());
a.WriteElementString("Humidity", forc.Humidity.ToString());
xml.WriteEndElement();
});
AddProperty(xml, weather);
}
}
xml.WriteEndElement();
xml.WriteEndDocument();
}
var xmlresult = xmlText.ToString();
How to set encoding to my XML to UTF-8? Please help...