I suspect that GZIP (via GZipStream or just through IIS, noting that you need to enable dynamic compression for the json-mime type) will be simpler and less, but if you use serializarion, just add some [XmlElement (...)] / [XmlAttribute (...)] should do this. Of course, if size bothers you, can I also suggest something like protobuf-net, which gives an extremely dense binary output.
If you are not using serialization, then this looks perfect 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);
source share