How to prevent XDocument from adding XML version and encoding information
That's the behaviour of the XDocument.Save
method when serializing to a file or a TextWriter
. If you want to omit the XML declaration, you can either use XmlWriter
(as shown below) or call ToString
. Refer to Serializing with an XML Declaration.
XDocument xmlDoc = XDocument.Load(FileManager.SourceFile); // perform your modifications on xmlDoc hereXmlWriterSettings xws = new XmlWriterSettings { OmitXmlDeclaration = true };using (XmlWriter xw = XmlWriter.Create(targetFile, xws)) xmlDoc.Save(xw);