I'm trying to output a xml file without xml head like I tried:
Type t = obj.GetType();
XmlSerializer xs=new XmlSerializer(t);
XmlWriter xw = XmlWriter.Create(@"company.xml",
new XmlWriterSettings() { OmitXmlDeclaration = true, Indent = true });
xs.Serialize(xw,obj);
xw.Close();
But it's still outputing in the xml file. I don't want string tricks. Any ideas?
Set the ConformanceLevel
to Fragment
, like this:
Type t = obj.GetType();
XmlSerializer xs=new XmlSerializer(t);
XmlWriter xw = XmlWriter.Create(@"company.xml",
new XmlWriterSettings() {
OmitXmlDeclaration = true
, ConformanceLevel = ConformanceLevel.Auto
, Indent = true });
xs.Serialize(xw,obj);
xw.Close();
Have a look in the documentation. There you see
The XML declaration is always written if ConformanceLevel is set to Document, even if OmitXmlDeclaration is set to true.
The XML declaration is never written if ConformanceLevel is set to Fragment. You can call WriteProcessingInstruction to explicitly write out an XML declaration.
So you need to add
ConformanceLevel = ConformanceLevel.Fragment;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With