コード例 #1
0
        public override void Save(string filename)
        {
            using (var fileStream = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.Write))
            {
                XmlWriter xmlWriter = null;

                try
                {
                    if (PreserveWhitespace)
                    {
                        XmlFormatter.Format(this);
                        xmlWriter = new XmlAttributePreservingWriter(fileStream, TextEncoding);
                    }
                    else
                    {
                        xmlWriter = new XmlTextWriter(fileStream, TextEncoding)
                        {
                            Formatting = Formatting.Indented
                        };
                    }

                    WriteTo(xmlWriter);
                }
                finally
                {
                    if (xmlWriter != null)
                    {
                        xmlWriter.Flush();
                        xmlWriter.Close();
                        xmlWriter.Dispose();
                        xmlWriter = null;
                    }
                }
            }
        }
コード例 #2
0
        public override void Save(Stream outStream)
        {
            XmlWriter xmlWriter = null;

            try
            {
                if (PreserveWhitespace)
                {
                    XmlFormatter.Format(this);
                    xmlWriter = new XmlAttributePreservingWriter(outStream, TextEncoding);
                }
                else
                {
                    xmlWriter = new XmlTextWriter(outStream, TextEncoding)
                    {
                        Formatting = Formatting.Indented
                    };
                }

                WriteTo(xmlWriter);
            }
            finally
            {
                if (xmlWriter != null)
                {
                    xmlWriter.Flush();
                    xmlWriter.Close();
                    xmlWriter.Dispose();
                    xmlWriter = null;
                }
            }
        }