Writing the Xml Declaration Using XmlTextWriter

  • Thread starter Thread starter Dan
  • Start date Start date
D

Dan

I'm trying to write an XmlDocument to disk using an XmlTextWriter.
Everything is fine except that I can't get the XML declaration to come out.
I'm using the code below. What am I doing wrong?

XmlTextWriter writer = new XmlTextWriter(m_FileName,
Encoding.Default);
writer.WriteStartDocument(true);
m_XMLDocument.WriteTo(writer);
writer.Flush();
writer.Close();
 
Here is what I ended up doing. Is this the best way to do this?

XmlDeclaration dec =
m_XMLDocument.CreateXmlDeclaration("1.0",string.Empty, "yes");
m_XMLDocument.InsertBefore((XmlNode)dec, (XmlNode)
m_XMLDocument.DocumentElement);
XmlTextWriter writer = new XmlTextWriter(m_FileName,
Encoding.Default);
writer.WriteRaw(m_XMLDocument.OuterXml);
writer.Flush();
writer.Close();
 
Back
Top