Ejemplo n.º 1
0
 public static string PrettyXmlSorted(StringBuilder sIn, XmlFormatSettings formatSettings, string[] exclusions)
 {
     exclude = new List <string>(exclusions)
     {
         ""
     };
     return(DoFormatXml(sIn, ref formatSettings, true, true));
 }
Ejemplo n.º 2
0
        private static string DoFormatXml(StringBuilder sIn, ref XmlFormatSettings formatSettings, bool doPretty, bool sorted)
        {
            var doc      = XDocument.Parse(sIn.ToString());
            var settings = new XmlWriterSettings()
            {
                OmitXmlDeclaration = formatSettings.IsSelection || (doc.Declaration == null)
            };

            if (!formatSettings.IsSelection && doc.Declaration != null)
            {
                string enc = doc.Declaration.Encoding;
                if (enc != "")
                {
                    try
                    {
                        settings.Encoding = Encoding.GetEncoding(doc.Declaration.Encoding);
                    }
                    catch { }
                }
            }

            settings.Indent = doPretty;
            if (doPretty)
            {
                string indent = "\t";
                if (!formatSettings.UseTabs)
                {
                    indent = "".PadRight(formatSettings.TabWidth);
                }

                settings.IndentChars  = indent;
                settings.NewLineChars = formatSettings.EolMode;
            }

            if (sorted)
            {
                doc = new XDocument(SortXmlElement(doc.Root));
            }

            using (var ms = new MemoryStream())
            {
                using (var xmlWriter = XmlWriter.Create(ms, settings))
                    doc.Save(xmlWriter);

                ms.Position = 0;
                using (var sr = new StreamReader(ms))
                    return(sr.ReadToEnd());
            }
        }
Ejemplo n.º 3
0
 public static string PrettyXml(StringBuilder sIn, XmlFormatSettings formatSettings)
 {
     return(DoFormatXml(sIn, ref formatSettings, true, false));
 }