Both of these examples leave the stream open, and the XmlFormatter is part of the BizTalk libraries - so the XmlSerializer would be more appropriate:
using (Stream stream = File.Open(fileName, FileMode.Create)) { XmlSerializer serializer = new XmlSerializer(typeof(MyObject)); serializer.Serialize(stream, MyObject); stream.Flush(); }
source share