public void Write(CircuitDocument document, Stream stream)
        {
            var context = new WriterContext();

            var xml = new XDocument(new XDeclaration("1.0", "utf-8", null),
                                    new XElement(Ns.Document + "circuit",
                                                 new XAttribute("version", Version),
                                                 CreateMetadata(document, context),
                                                 CreateDefinitions(document, context),
                                                 CreateElements(document, context)));

            var writer = XmlWriter.Create(stream, new XmlWriterSettings
            {
                Encoding    = Encoding.UTF8,
                Indent      = true,
                IndentChars = "\t"
            });

            xml.WriteTo(writer);
            writer.Flush();
        }