13

I am creating a Xml like format using XmlWriter. But in the output there is version information also.

<?xml version="1.0" encoding="utf-8"?>

I don't need this in my file. How can I do that? Is there any way to remove it by code?

viky
  • 17,275
  • 13
  • 71
  • 90
  • 9
    Stop! Please! Don't make "XML-like formats". Use real XML. If it is XML-like then another developer will come along, look at it, assume it is XML, and then have problems when they try to parse it as XML. (And once you use real XML, the use of the processing instruction won't be an issue, since the values given there are the defaults that are assumed when that processing instruction is missing). – Quentin Dec 30 '09 at 13:27
  • 2
    That's the XML declaration required by the specification. Why do you want to remove it? (The declaration is optional in XML 1.0 but required in XML 1.1) – Dirk Vollmar Dec 30 '09 at 13:28
  • Also, the information you're looking for is contained in the question _Omitting XML processing instruction when serializing an object_ (http://stackoverflow.com/questions/164585/omitting-xml-processing-instruction-when-serializing-an-object) – Blair Conrad Dec 30 '09 at 13:28
  • 1
    @divo It isn't required. It is optional (so long as you use 1.0 and UTF-8 or UTF-16) – Quentin Dec 30 '09 at 13:29
  • We are using a custom language, so it is just Xml-like but not the xml, We have a custom parser for parsing this format which has nothing to do with this processing instruction so doesn't need this. – viky Dec 30 '09 at 13:34

3 Answers3

19

Use the ConformanceLevel and OmitXmlDeclaration properties. Example:

XmlWriter w;
w.Settings = new XmlWriterSettings();
w.Settings.ConformanceLevel = ConformanceLevel.Fragment;
w.Settings.OmitXmlDeclaration = true;
Aviad P.
  • 32,036
  • 14
  • 103
  • 124
  • why should use ConformanceLevel it is working without setting w.Settings.ConformanceLevel = ConformanceLevel.Fragment; also. What is ConformanceLevel? – viky Dec 31 '09 at 09:23
  • 1
    The `Fragment` conformance level means you're not writing an entire document, you're writing a fragment. The documentation says that setting `OmitXmlDeclaration` to `true` would not have any effect if the `ConformanceLevel` is set at `Document`. – Aviad P. Dec 31 '09 at 10:10
  • Just to note that you do not need the have the line `w.Settings.ConformanceLevel = ConformanceLevel.Fragment;` It will throw an error if you also use `writer.WriteStartDocument();` . It could be argued that you shouldn't be using it this way anyway, but it works as long as you leave it as `settings.ConformanceLevel = ConformanceLevel.Auto;`. – Ravendarksky Nov 23 '15 at 16:57
13

When creating your XmlWriter, pass through the settings you want using XmlWriterSettings:

XmlWriterSettings settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;

writer = XmlWriter.Create(Console.Out, settings);

XmlWriterSettings has other properties as well (indent and more).

Oded
  • 489,969
  • 99
  • 883
  • 1,009
0

You can use

XmlWriterSettings class

and use XmlWriterSettings.OmitXmlDeclaration Property

rahul
  • 184,426
  • 49
  • 232
  • 263