-
Notifications
You must be signed in to change notification settings - Fork 48
XML Minifier
Taritsyn edited this page Dec 2, 2022
·
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>https://webmarkupmin.bsite.net/</loc>
<changefreq>hourly</changefreq>
<priority>0.9</priority>
</url>
<url>
<loc>https://webmarkupmin.bsite.net/minifiers</loc>
<changefreq>daily</changefreq>
<priority>0.7</priority>
</url>
<url>
<loc>https://webmarkupmin.bsite.net/change-log</loc>
<changefreq>daily</changefreq>
<priority>0.8</priority>
</url>
<url>
<loc>https://webmarkupmin.bsite.net/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} 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>https://webmarkupmin.bsite.net/</loc>
<changefreq>hourly</changefreq>
<priority>0.9</priority>
</url>
<url>
<loc>https://webmarkupmin.bsite.net/minifiers</loc>
<changefreq>daily</changefreq>
<priority>0.7</priority>
</url>
<url>
<loc>https://webmarkupmin.bsite.net/change-log</loc>
<changefreq>daily</changefreq>
<priority>0.8</priority>
</url>
<url>
<loc>https://webmarkupmin.bsite.net/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.GetEncoding(0),
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. |
PreserveNewLines |
Boolean |
false |
Flag for whether to collapse whitespace to one newline string when whitespace contains a newline. |
NewLineStyle |
NewLineStyle enumeration |
Auto |
Style of the newline. Can take the following values:
|
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/> ).
|
- Core
- External JS and CSS minifiers
- ASP.NET Extensions
- How to upgrade applications to version 2.X
- Additional reading and resources