views:

66

answers:

1

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.

+3  A: 

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"&gt;
  <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);
Marc Gravell
Thanks. But I am getting this XML as input from an SWF and .NET part is to minify(or rather pack) the xml n then convert to json so that size is much smaller. I am saving the JSON at another location for another SWF to access it.
naveen
@naveen - the xslt route, then (example added)
Marc Gravell
@Marc: Thanks a lot pal
naveen
@naveen well you could use LINQ-XML, but xslt is handy (designed for) transforming XML.
Marc Gravell
@Marc: Thanks for the snippet. I really was mentioning that.
naveen