I have an XML file on a given location and I want to have the content of the XML in a string variable. However, when I print it out, the encoding changes. This is how I am doing it.
XmlDocument xmlFile = new XmlDocument();
xmlFile.Load(xmlFileLocation);
using (StringWriter stringWriter = new StringWriter(new StringBuilder()))
{
using (XmlTextWriter xmlTextWriter = new XmlTextWriter(stringWriter) { Formatting = Formatting.Indented })
{
xmlFile.Save(xmlTextWriter);
}
return stringWriter.ToString();
}
When I try to initialize the XmlTextWriter (new XmlTextWriter(stringWriter, Encoding.UTF8)) with a given encoding, then I have issues with the StringWriter but I don't know how to sort it out. Any idea?
The issue is: Cannot convert from StringWriter into Stream