missing xml line

  • Thread starter Thread starter Jack
  • Start date Start date
J

Jack

I wrote the following codes to write an XML document. The file output is
fine except it missed out the line <?xml version="1.0"?>. How do I add it ?

//load the xml file
XmlDocument doc = new XmlDocument();

doc.AppendChild(serializer.WriteConfiguration(doc, config));
XmlTextWriter writer = new XmlTextWriter(file,
System.Text.Encoding.UTF8);
doc.WriteTo(writer);
writer.Close();
 
Hi,

You can do it two ways,

a) The "crude":

doc.LoadXml("<?xml version="1.0" encoding="iso-8859-1"?>" +
doc.OuterXml);

b) More elegantly:

XmlDeclaration decl = doc.CreateXmlDeclaration("1.0", "ISO-8859-1",
null);
doc.InsertBefore (decl, doc.DocumentElement);

Regards - Octavio
 
Back
Top