I'm using XmlWriter and XmlWriterSettings to write an XML file to disk. However, the system that is parsing the XML file is complaining about the encoding.
<?xml version="1.0" encoding="utf-8"?>
What it wants is this:
<?xml version="1.0" ?>
If I try OmitXmlDeclaration = true, then I don't get the xml line at all.
string destinationName = "C:\\temp\\file.xml";
string strClassification = "None";
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
using (XmlWriter writer = XmlWriter.Create(destinationName, settings))
{
writer.WriteStartDocument();
writer.WriteStartElement("ChunkData");
writer.WriteElementString("Classification", strClassification);
writer.WriteEndElement();
}
Just ran into this ---
XmlWriterSettings()
altogether and use the XmlTextWriter()'s Formatting
field for indention. null
for the Encoding
argument of XmlTextWriter's ctor
The following code will create the output that you're looking for:
<?xml version="1.0" ?>
var w = new XmlTextWriter(filename, null);
w.Formatting = Formatting.Indented;
w.WriteStartDocument();
w.WriteStartElement("ChunkData");
w.WriteEndDocument();
w.Close();
The .Close()
effectively creates the file - the using
and Create()
approach will also work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With