mortb wrote:
1. How do I determine which encoding a xmldocument or xmlreader uses when
opening a document?
I'm not just talking about the <?xml encoding="utf-8"?attribute, but the
actual encoding of the characters in the underlying stream.
I don't understand that question. If the encoding is delcared in the XML
declaration (e.g. <?xml version="1.0" encoding="UTF-8"?>) then the
characters in the underlying stream have to encoded with UTF-8,
otherwise the document is not well-formed.
As for reading out the encoding in the XML declaration, XmlReader allows
it with e.g. NET 2.0 C#
using (XmlReader xmlReader = XmlReader.Create(@"file.xml")) {
if (xmlReader.Read() && xmlReader.NodeType ==
XmlNodeType.XmlDeclaration) {
Console.WriteLine("Declared encoding is: {0}.",
xmlReader.GetAttribute("encoding"));
}
else {
Console.WriteLine("No XML declaration found.");
}
}
That obviously only helps if the XML document has an XML declaration
declaring the encoding. If not the XML is either UTF-8 or UTF-16
encoded, depending on the presence and form of a byte order mark at the
very beginning.
If you are not working with XmlReader but with XmlDocument then you will
also find the XmlDeclaration node in the DOM tree e.g.
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(@"file.xml");
if (xmlDocument.FirstChild.NodeType ==
XmlNodeType.XmlDeclaration) {
XmlDeclaration declaration = xmlDocument.FirstChild as
XmlDeclaration;
Console.WriteLine("Declared encoding is: {0}.",
declaration.Encoding);
}
else {
Console.WriteLine("No XML declaration found.");
}
2. How do I make sure that the encoding of my created xmldocument or
xmlwriter is in utf-8?
As already pointed out, you can specify the encoding to use when
creating the XmlWriter e.g. in .NET 2.0 with the XmlWriterSettings e.g.
XmlWriterSettings writerSettings = new XmlWriterSettings();
writerSettings.Encoding = System.Text.Encoding.UTF8;
using (XmlWriter xmlWriter = XmlWriter.Create(@"file.xml",
writerSettings) {
xmlWriter.WriteStartDocument();
...
}
But UTF-8 is pretty much the default, for instance when the Save method
of an XmlDocument method is being called and there is no XML declaration
or there is one but no encoding is specified then UTF-8 is used.
--
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/