Skip to content

XML Minifier

Andrey Taritsyn edited this page Aug 10, 2015 · 8 revisions

XML Minifier produces minification of XML code.

Consider a simple example of usage of the XML Minifier:

using System;
using System.Collections.Generic;

using WebMarkupMin.Core;

namespace WebMarkupMin.Sample.ConsoleApplication
{
	class Program
	{
		private static void Main(string[] args)
		{
			const string xmlInput = @"<?xml version=""1.0"" encoding=""utf-8"" standalone=""yes""?>
<urlset xmlns=""http://www.sitemaps.org/schemas/sitemap/0.9"">
	<url>
		<loc>http://webmarkupmin.apphb.com/</loc>
		<changefreq>hourly</changefreq>
		<priority>0.9</priority>
	</url>
	<url>
		<loc>http://webmarkupmin.apphb.com/minifiers</loc>
		<changefreq>daily</changefreq>
		<priority>0.7</priority>
	</url>
	<url>
		<loc>http://webmarkupmin.apphb.com/change-log</loc>
		<changefreq>daily</changefreq>
		<priority>0.8</priority>
	</url>
	<url>
		<loc>http://webmarkupmin.apphb.com/contact</loc>
		<changefreq>weekly</changefreq>
		<priority>0.4</priority>
	</url>
</urlset>";

			var xmlMinifier = new XmlMinifier();

			MarkupMinificationResult result = xmlMinifier.Minify(xmlInput,
				generateStatistics: true);
			if (result.Errors.Count == 0)
			{
				MinificationStatistics statistics = result.Statistics;
				if (statistics != null)
				{
					Console.WriteLine("Original size: {0:N0} Bytes",
						statistics.OriginalSize);
					Console.WriteLine("Minified size: {0:N0} Bytes",
						statistics.MinifiedSize);
					Console.WriteLine("Saved: {0:N2}%",
						statistics.SavedInPercent);
				}
				Console.WriteLine("Minified content:{0}{0}{1}",
					Environment.NewLine, result.MinifiedContent);
			}
			else
			{
				IList<MinificationErrorInfo> errors = result.Errors;

				Console.WriteLine("Found {0:N0} error(s):", errors.Count);
				Console.WriteLine();

				foreach (var error in errors)
				{
					Console.WriteLine("Line {0}, Column {1}: {2}",
						error.LineNumber, error.ColumnNumber, error.Message);
					Console.WriteLine();
				}
			}
		}
	}
}

The above code does not need any comment, because it is very similar to analogous example for the HTML Minifier.

Consider an example of a more advanced usage of the XML Minifier:

using System;
using System.Collections.Generic;
using System.Text;

using WebMarkupMin.Core;
using WebMarkupMin.Core.Loggers;

namespace WebMarkupMin.Sample.ConsoleApplication
{
	class Program
	{
		static void Main(string[] args)
		{
			const string xmlInput = @"<?xml version=""1.0"" encoding=""utf-8"" standalone=""yes""?>
<urlset xmlns=""http://www.sitemaps.org/schemas/sitemap/0.9"">
	<url>
		<loc>http://webmarkupmin.apphb.com/</loc>
		<changefreq>hourly</changefreq>
		<priority>0.9</priority>
	</url>
	<url>
		<loc>http://webmarkupmin.apphb.com/minifiers</loc>
		<changefreq>daily</changefreq>
		<priority>0.7</priority>
	</url>
	<url>
		<loc>http://webmarkupmin.apphb.com/change-log</loc>
		<changefreq>daily</changefreq>
		<priority>0.8</priority>
	</url>
	<url>
		<loc>http://webmarkupmin.apphb.com/contact</loc>
		<changefreq>weekly</changefreq>
		<priority>0.4</priority>
	</url>
</urlset>";

			var settings = new XmlMinificationSettings();
			var logger = new NullLogger();

			var xmlMinifier = new XmlMinifier(settings, logger);

			MarkupMinificationResult result = xmlMinifier.Minify(xmlInput,
				 fileContext: string.Empty, encoding: Encoding.Default,
				 generateStatistics: false);
			if (result.Errors.Count == 0)
			{
				Console.WriteLine("Minified content:{0}{0}{1}",
					 Environment.NewLine, result.MinifiedContent);
			}
			else
			{
				IList<MinificationErrorInfo> errors = result.Errors;

				Console.WriteLine("Found {0:N0} error(s):", errors.Count);
				Console.WriteLine();

				foreach (var error in errors)
				{
					Console.WriteLine("Line {0}, Column {1}: {2}",
						 error.LineNumber, error.ColumnNumber, error.Message);
					Console.WriteLine();
				}
			}
		}
	}
}

This example is also very similar to analogous example for the HTML Minifier. Its main difference is that when you create an instance of the XmlMinifier class, there is no need to pass CSS and JS minifiers to its constructor.

Let's consider in detail properties of the XmlMinificationSettings class:

Property name Data type Default value Description
MinifyWhitespace Boolean true Flag for whether to minify whitespace.
RemoveXmlComments Boolean true Flag for whether to remove all XML comments.
RenderEmptyTagsWithSpace Boolean false Flag for whether to allow the inserting space before slash in empty tag.
CollapseTagsWithoutContent Boolean false Flag for whether to collapse tags without content (for example, <node></node> is transforms to <node/>).