codeandcloud
codeandcloud

Reputation: 55200

Optimal Method to Minify an XML in C# 3.0

Coding Platform: ASP.NET C#

I have an XML like this.

<Items>
    <Map id="35">
        <Terrains>
            <Item id="1" row="0" column="0"/>
            <Item id="1" row="0" column="1"/>
            <Item id="1" row="0" column="2"/>
            <Item id="1" row="0" column="3"/>
            <Item id="1" row="0" column="4"/>
        </Terrains>
    </Map>
</Items>

I would like to minify this to

<Its>
    <Map id="30">
        <Te>
            <It id="1" r="0" c="0"/>
            <It id="1" r="0" c="1"/>
            <It id="1" r="0" c="2"/>
            <It id="1" r="0" c="3"/>
            <It id="1" r="0" c="4"/>
        </Te>
    </Map>
</Its>

Then I am converting this to JSON using James Newton-King's JSON Converter.
The idea is to minify the xml data to the maximum as it contains tens of thousands of lines.

My questions are

  1. What is the optimal method to minify the xml as mentioned above?
  2. Now its done like XML-MinifyXML-Convert to JSON. Can I do it in two steps?(XML-Minify while converting to JSON)
  3. Is James Newton-King's JSON converter a bit overkill for this simple conversion?

Please provide code snippets also if possible.

Upvotes: 4

Views: 2448

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1062590

I suspect GZIP (via GZipStream, or simply via IIS, noting that you need to enable dynamic compression for the json mime-type) would be both simpler and smaller, but if you are using serializarion, simply adding some [XmlElement(...)] / [XmlAttribute(...)] should do it. Of course, if size is your concern, can I also suggest something like protobuf-net, which gives an extremely dense binary output.

If you aren't using serialisation, then this looks an ideal fit for some "xslt":

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="@* | node()">
    <xsl:copy><xsl:apply-templates select="@* | node()"/></xsl:copy>
  </xsl:template>
  <xsl:template match="/Items">
    <Its><xsl:apply-templates/></Its>
  </xsl:template>
  <xsl:template match="/Items/Map/Terrains">
    <Te><xsl:apply-templates/></Te>
  </xsl:template>
  <xsl:template match="/Items/Map/Terrains/Item">
    <It id="{@id}" r="{@row}" c="{@column}"><xsl:apply-templates select="*"/></It>
  </xsl:template>
</xsl:stylesheet>

(with C#:)

XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load("Condense.xslt"); // cache and re-use this object; don't Load each time
xslt.Transform("Data.xml", "Smaller.xml");

 Console.WriteLine("{0} vs {1}",
    new FileInfo("Data.xml").Length,
    new FileInfo("Smaller.xml").Length);

Upvotes: 5

Related Questions