I am using XmlWriter.Create()
to get a writer instance then write the XML, but the result has the <?xml version="1.0" encoding="utf-16" ?>
, how do I tell my xml writer do not produce it?
Asked
Active
Viewed 2.3k times
25

Kirill Polishchuk
- 54,804
- 11
- 122
- 125

ninithepug
- 253
- 1
- 3
- 4
3 Answers
37
Use XmlWriterSettings.OmitXmlDeclaration
.
Don't forget to set XmlWriterSettings.ConformanceLevel
to ConformanceLevel.Fragment
.

Sebastian Brosch
- 42,106
- 15
- 72
- 87

Kirill Polishchuk
- 54,804
- 11
- 122
- 125
-
From the docs you link to: `The XML declaration is always written if ConformanceLevel is set to Document, even if OmitXmlDeclaration is set to true` – Cameron Jul 26 '11 at 16:50
-
Won't it be set to Document most of the time? – Cameron Jul 26 '11 at 16:56
-
@Cameron, Strange question... `ConformanceLevel.Document` is default, but you can set `ConformanceLevel.Fragment`... – Kirill Polishchuk Jul 26 '11 at 16:58
-
Ah, yes, I understand know :-) You might want to add that into your answer. What about if you want the Document conformance level (e.g. to ensure a single root node), but no XML declaration? – Cameron Jul 26 '11 at 17:02
-
@Cameron, I think you answered your your question yourself :-) – Kirill Polishchuk Jul 26 '11 at 17:05
-
Kirill, forgive my ignorance, but how did @Cameron answer the question himself? – ro͢binmckenzie Jul 19 '16 at 09:10
-
@ro͢binmckenzie: Oh, it looks like I literally answered this question with an alternate solution (below). – Cameron Jul 19 '16 at 15:57
-
In .NET Core 3.1 at least, using `OmitXmlDeclaration = false` with `ConformanceLevel.Default` works fine and no XML Declaration header is written (when using `XmlDocument.Save`). – Dai Jun 18 '21 at 14:22
6
You can subclass XmlTextWriter
and override the WriteStartDocument()
method to do nothing:
public class XmlFragmentWriter : XmlTextWriter
{
// Add whichever constructor(s) you need, e.g.:
public XmlFragmentWriter(Stream stream, Encoding encoding) : base(stream, encoding)
{
}
public override void WriteStartDocument()
{
// Do nothing (omit the declaration)
}
}
Usage:
var stream = new MemoryStream();
var writer = new XmlFragmentWriter(stream, Encoding.UTF8);
// Use the writer ...
Reference: This blog post from Scott Hanselman.

Cameron
- 96,106
- 25
- 196
- 225
-
thanks, any better way? I do not want to subclass just for the purpose of removing the declaration. – ninithepug Jul 26 '11 at 16:54
-
@ninithepug: Not as far as I know, sorry. You can wrap it up in a static method somewhere if you use it often, though. That should help keep it clean – Cameron Jul 26 '11 at 17:00
-
@ninithepug: It seems Kirill's answer (which doesn't need a new class) might be better (unless you want the Document conformance level, of course). – Cameron Jul 26 '11 at 17:08
2
you can use XmlWriter.Create()
with:
new XmlWriterSettings {
OmitXmlDeclaration = true,
ConformanceLevel = ConformanceLevel.Fragment
}
public static string FormatXml(string xml)
{
if (string.IsNullOrEmpty(xml))
return string.Empty;
try
{
XmlDocument document = new XmlDocument();
document.LoadXml(xml);
using (MemoryStream memoryStream = new MemoryStream())
using (XmlWriter writer = XmlWriter.Create(
memoryStream,
new XmlWriterSettings {
Encoding = Encoding.Unicode,
OmitXmlDeclaration = true,
ConformanceLevel = ConformanceLevel.Fragment,
Indent = true,
NewLineOnAttributes = false }))
{
document.WriteContentTo(writer);
writer.Flush();
memoryStream.Flush();
memoryStream.Position = 0;
using (StreamReader streamReader = new StreamReader(memoryStream))
{
return streamReader.ReadToEnd();
}
}
}
catch (XmlException ex)
{
return "Unformatted Xml version." + Environment.NewLine + ex.Message;
}
catch (Exception ex)
{
return "Unformatted Xml version." + Environment.NewLine + ex.Message;
}
}

ro͢binmckenzie
- 325
- 3
- 11

IUB
- 47
- 6