Best way to minimize XML in C # 3.0

Coding Platform: ASP.NET C #

I have such XML.

<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 reduce 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 convert it to JSON using James Newton-King JSON Converter .
The idea is to minimize xml data as much as it contains tens of thousands of lines.

My questions

  • What is the best method to minimize xml as above?
  • Now this is done as XML-MinifyXML-Convert to JSON. Can I do this in two steps? (XML-Minify when converting to JSON)
  • Is the James Newton-King JSON Converter a bit redundant for this simple conversion?

Please also provide code snippets, if possible.

+4
source share
1 answer

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); 
+5
source

All Articles