Search code examples
c#.netxmlreaderxelement

How to omit the xml declarations when using XElement.Save?


XElement.Save actually does what I need but it starts the file with:

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

Is there a way to prevent this?

Should I save using other types, methods after I finish creating my XElement?

Or should I be skipping that line via XmlReader.Read? Because doing this I feel like it's more fragile as I am assuming the first line is always gonna be this xml declaration.

What's the simpliest way to accomplish this?


Solution

  • XElement.ToString() won't add the XML declaration to the output. But I don't understand why XmlReader - or any XML parser - would have trouble with a standard XML declaration.