1) The text at the top is written by the XMLWriter, not the StreamWriter, so the written encoding of the XML and the logical encoding of the file are two seperate things.
So, use the XMLWriter override of XTypedElement.Save(), create an XMLTextWriter (XMLWriter is just an abstract class) and set it's Settings property which takes an XMLWriterSettings instance. This has an Encoding propery, which tells the XMLWriter what to
write in the XML declaration. However, it looks like it takes it default from the underlying TextWriter anyway (I'm reading the docs as I write this). So using the StreamWriter without encoding defaults to utf8.
2) No idea. Cheat, write it to a string first and use a Regex?
3) That's a BOM, a Byte Order Mark, in your case it's: 0xEF, 0xBB, 0xEF. There shouldn't be one on utf8 as it breaks the utf8/ASCII backwards compatibility (or it's recommended not to have one)
From the docs:
"StreamWriter defaults to using an instance of
UTF8Encoding unless specified otherwise. This instance of UTF8Encoding is constructed without a byte order mark (BOM), so its
GetPreamble method returns an empty byte array. To create a StreamWriter using UTF-8 encoding and a BOM, consider using a constructor that specifies encoding, such as
StreamWriter(String, Boolean, Encoding)."
Looks like it's the constructor you're using? Guess you'll have to figure out how to switch the BOM off.
Here's a bunch of stuff to read about:
Stream Writers and BOMs: