nikmd23
nikmd23

Reputation: 9103

XML Minify in .NET

I'd like to read in the following XML:

<node></node>

And then write it out, minified, like this:

<node/>

Obviously this has the same meaning, but the second file is smaller for sending across the wire.

I'm trying to find a way to do this in .NET. I can't seem to find an option or setting that would drop unnecessary closing tags.

Suggestions?

Upvotes: 3

Views: 2786

Answers (6)

Dimitri Sluzki
Dimitri Sluzki

Reputation: 71

private void start_xmlButton_Click(object sender, EventArgs e)
{
    try
    {
        OpenFileDialog openFileDialog = new OpenFileDialog();
        if (openFileDialog.ShowDialog() == DialogResult.OK)
        {
            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.Load(openFileDialog.FileName);
            var minify_xml_content = xmlDocument.OuterXml;
        }
    }
    catch (Exception ex)
    {                
        gErrors.DisplayError(this, null, ex,
            ohaERP_Library.Properties.Settings.Default.LoggingPerEmail,
            ohaERP_Library.Properties.Resources.msgCaptionError + " in: " + ToString(), g_client_name);
    }
}

Upvotes: 0

Andrey Taritsyn
Andrey Taritsyn

Reputation: 1286

Try a WebMarkupMin XML Minifier with the option "Collapse tags without content":

    const string xmlInput = "<row RoleId=\"4\" RoleName=\"Administrator\"></row>\n" +
        "<row RoleId=\"5\" RoleName=\"Contributor\"></row>\n" +
        "<row RoleId=\"6\" RoleName=\"Editor\"></row>"
        ;

    var xmlMinifier = new XmlMinifier(
        new XmlMinificationSettings{ CollapseTagsWithoutContent = true });

    MarkupMinificationResult result = xmlMinifier.Minify(xmlInput);

    Console.WriteLine("Minified content:{0}{0}{1}",
        Environment.NewLine, result.MinifiedContent);

Upvotes: 0

David
David

Reputation: 73554

I didn't test this myself, but have you tried experimenting with the XmlWriter's XmlWriterSettings.OutputMethod Property?

The following page gives you the options you can use:

http://msdn.microsoft.com/en-us/library/system.xml.xmloutputmethod.aspx

Upvotes: 0

Bob
Bob

Reputation: 99694

You can copy the XML into a new structure.

public static XElement Minify(XElement element) {
    return new XElement(element.Name, element.Attributes(),
        element.Nodes().Select(node => {
            if (node is XElement)
                return Minify((XElement)node);
            return node;
        })
    );
}

Here is another solution but LINQ-less http://social.msdn.microsoft.com/Forums/en-US/xmlandnetfx/thread/e1e881db-6547-42c4-b379-df5885f779be

XmlDocument doc = new XmlDocument();
doc.PreserveWhitespace = true;
doc.Load("input.xml");
foreach (XmlElement el in 
   doc.SelectNodes("descendant::*[not(*) and not(normalize-space())]"))
{
  el.IsEmpty = true;
}
doc.Save("output.xml");

Upvotes: 2

zac
zac

Reputation: 961

Take a look at the XmlElement IsEmpty property.

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1499950

If you use LINQ to XML, you can call XElement.RemoveNodes() which will convert it to the second form. So something like this:

var emptyTags = doc.Descendants().Where(x => !x.Nodes().Any()).ToList();

foreach (XElement tag in emptyTags)
{
    tag.RemoveNodes();
}

Then save the document in the normal way, and I think it will do what you want...

Upvotes: 2

Related Questions