Here are a few thoughts for loading and storing your xml using the
XmlDocument class:
First, don't ever insert the header manually. This is to be done by
the writer. In other words, don't do this:
document.AppendChild(document.CreateXmlDeclaration ("1.0", "UTF-8",
null));
If you do that and then write your document to a stream with an
encoding other than UTF8, well uh, you're skrewed at that point. If
you do anything other than a document.Save(filename) at that point
your're skrewed. And if you weren't expecting the byte ordering marks
in whatever you're using to read that document, you're skrewed.
Instead, save your document to a file this way:
using(var writer = new XmlTextWriter(blah, blah)){
document.Save(writer); writer.Close(); }
That will automagically insert the appropriate xml header for you. And
it won't stick in byte ordering marks (a BOM) without you specifically
telling it to do so in the XmlTextWriter params.
If you want to write your document to a string, save to a the
StringWriter. It will automatically add an xml header of type UTF-16
and it won't stick in that nasty BOM. On top of that, strings in C#
are UTF16 -- that means your header actually matches the storage
medium. Amazing!
As a note, XmlDocument.Load* methods actually use the XmlTextReader
and StringReader stuff appropriately so there is little to be gained
by not using the XmlDocument.Load methods.