views:

264

answers:

3

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?

A: 

You can use

XmlWriterSettings class

and use XmlWriterSettings.OmitXmlDeclaration Property

rahul
+6  A: 

Use the ConformanceLevel and OmitXmlDeclaration properties. Example:

XmlWriter w;
w.Settings = new XmlWriterSettings();
w.Settings.ConformanceLevel = ConformanceLevel.Fragment;
w.Settings.OmitXmlDeclaration = true;
Aviad P.
why should use ConformanceLevel it is working without setting w.Settings.ConformanceLevel = ConformanceLevel.Fragment; also. What is ConformanceLevel?
viky
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.
A: 

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