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;
                }
            }
        }
        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;
                    }
                }
            }
        }
Ejemplo n.º 3
0
        internal void WritePreservedAttributes(XmlAttributePreservingWriter writer, XmlAttributeCollection attributes)
        {
            string oldNewLineString = null;

            if (_attributeNewLineString != null)
            {
                oldNewLineString = writer.SetAttributeNewLineString(_attributeNewLineString);
            }

            try
            {
                foreach (string attributeName in _orderedAttributes)
                {
                    XmlAttribute attr = attributes[attributeName];
                    if (attr != null)
                    {
                        if (_leadingSpaces.ContainsKey(attributeName))
                        {
                            writer.WriteAttributeWhitespace(_leadingSpaces[attributeName]);
                        }

                        attr.WriteTo(writer);
                    }
                }

                if (_leadingSpaces.ContainsKey(string.Empty))
                {
                    writer.WriteAttributeTrailingWhitespace(_leadingSpaces[string.Empty]);
                }
            }
            finally
            {
                if (oldNewLineString != null)
                {
                    writer.SetAttributeNewLineString(oldNewLineString);
                }
            }
        }