all groups > dotnet xml > january 2006 >
You're in the

dotnet xml

group:

how to include the XML declaration with XmlDocument.WriteTo()



Re: how to include the XML declaration with XmlDocument.WriteTo() DSiders
1/12/2006 9:00:04 AM
dotnet xml: [quoted text, click to view]

You have a choice: create the PI in the XmlDocument, or add the PI using the
XmlTextWriter.

If the XmlDocument does not already contain a processing instruction for the
XML declaration, then WriteStartDocument is the way to ensure that one is
created in the XmlTextWriter. See the XmlDocument.CreateXmlDeclaration
method for the alternate mechanism.

Be careful about encodings values too. The encoding in the XmlDocument is
not automatically used for the XmlTextWriter (unless both happen to be
UTF-8).

hth...


how to include the XML declaration with XmlDocument.WriteTo() Andy Fish
1/12/2006 11:10:35 AM
Hi,

I am using XmlDocument.WriteTo() and I find that the xml declaration is not
included.

at the moment I am doing this:

XmlTextWriter xw = new XmlTextWriter(...);
xw.WriteStartDocument();
doc.WriteTo(xw);

Which works, but it seems a bit cludgy. is this the correct solution?

Andy

Re: how to include the XML declaration with XmlDocument.WriteTo() Martin Honnen
1/12/2006 2:59:41 PM


[quoted text, click to view]

Does the XmlDocument have an XmlDeclaration child node?

For me with .NET 1.1 the following

XmlDocument xmlDocument = new XmlDocument();
xmlDocument.AppendChild(xmlDocument.CreateXmlDeclaration("1.0", "",
""));
xmlDocument.AppendChild(xmlDocument.CreateElement("gods"));
XmlTextWriter xmlWriter = new XmlTextWriter(Console.Out);
xmlDocument.WriteTo(xmlWriter);
xmlWriter.Flush();
xmlWriter.Close();

produces

<?xml version="1.0"?><gods />

so in that case the XML declaration is being written.

--

Martin Honnen --- MVP XML
Re: how to include the XML declaration with XmlDocument.WriteTo() Andy Fish
1/13/2006 9:53:03 AM

[quoted text, click to view]

Thanks to both for these replies. I guess I thought that the xml declaration
was an implicit part of the serialisation rather than being an explicit PI.

Re: how to include the XML declaration with XmlDocument.WriteTo() Chris Lovett
1/15/2006 1:08:38 AM
The xml declaration is omitted if the document defaults to UTF-8. The
XmlTextWriter will write the xml declaration if you specify some other
encoding. But you should use UTF-8 and having no xml declaration is
perfectly legal.

[quoted text, click to view]

Re: how to include the XML declaration with XmlDocument.WriteTo() DSiders
1/15/2006 12:57:57 PM
[quoted text, click to view]

Ok. But the OP specifically *wanted* an XML declaration.

AddThis Social Bookmark Button