/// <summary>
        /// Orders and beautifies an XML document.
        /// </summary>
        /// <param name="source">The source XML document to order and beautify.</param>
        /// <param name="extension">The extension.  Used to determine tab spacing.</param>
        /// <returns>The text of the scrubbed and beautified document.</returns>
        private static string ScrubDocument(string source, string extension)
        {
            // Used to write the final document in the form of a long string.
            using (StringWriter stringWriter = new StringWriter(CultureInfo.InvariantCulture))
            {
                try
                {
                    // Read the source document.
                    using (StringReader stringReader = new StringReader(source))
                    {
                        // Create an XML document from the source string.
                        XDocument sourceDocument = XDocument.Load(stringReader, LoadOptions.PreserveWhitespace);

                        // Create a new target document.
                        XDocument targetDocument = new XDocument();

                        // Copy the declaration.
                        if (sourceDocument.Declaration != null)
                        {
                            targetDocument.Declaration = new XDeclaration(sourceDocument.Declaration);
                        }

                        // This will order all the attributes in the document in alphabetical order.  Note that the elements can't be similarly
                        // ordered because this could produce forward reference errors.
                        FormatXmlCommand.RecurseIntoDocument(sourceDocument.Root, targetDocument);

                        // This is used to make special consideration for XALM files.
                        bool isXaml = extension == ".XAML";

                        // Beautify and save the target document when it has been ordered.
                        XmlFormattedWriterSettings xmlFormattedWriterSettings = new XmlFormattedWriterSettings
                        {
                            Encoding           = Encoding.UTF8,
                            OmitXmlDeclaration = isXaml,
                            TabSize            = isXaml ? 4 : 2,
                        };
                        XmlWriter xmlWriter = XmlFormattedWriter.Create(stringWriter, xmlFormattedWriterSettings);
                        targetDocument.WriteTo(xmlWriter);
                        xmlWriter.Close();
                    }
                }
                catch (Exception exception)
                {
                    // Show a message box to prove we were here
                    VsShellUtilities.ShowMessageBox(
                        FormatXmlCommand.serviceProvider,
                        exception.Message,
                        Resources.EditorExtensionsTitle,
                        OLEMSGICON.OLEMSGICON_CRITICAL,
                        OLEMSGBUTTON.OLEMSGBUTTON_OK,
                        OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST);
                }

                // The result of beautifying a long string of XML is another long string that is scrubbed and beautified.  This string will be used
                // to replace the original content of the module.
                return(stringWriter.ToString());
            }
        }
Beispiel #2
0
        /// <summary>
        /// Creates a new <see cref="XmlFormattedWriter"/> instance using the <see cref="TextWriter"/> and <see cref="XmlFormattedWriterSettings"/>
        /// objects.
        /// </summary>
        /// <param name="textWriter">The <see cref="TextWriter"/> to which you want to write.</param>
        /// <param name="xmlFormattedWriterSettings">
        /// The <see cref="XmlFormattedWriterSettings"/> object used to configure the new <see cref="TextWriter"/> instance.  If this is null, a
        /// <see cref="XmlFormattedWriterSettings"/> with default settings is used.
        /// </param>
        /// <returns>An <see cref="XmlFormattedWriter"/> object.</returns>
        internal static XmlWriter Create(TextWriter textWriter, XmlFormattedWriterSettings xmlFormattedWriterSettings)
        {
            XmlFormattedWriter xmlFormattedTextWriter = new XmlFormattedWriter(textWriter, xmlFormattedWriterSettings);

            return(XmlWriter.Create(xmlFormattedTextWriter, xmlFormattedWriterSettings.BaseSettings));
        }