Alex
Alex

Reputation: 9720

How to reduce size of xml file programatically in c#

I have one xml file, it has 110KB I've uploaded it here

In Notepad++ I'm using XML Tools plugin and pretty print (Ctrl+Alt+Shift+B) for code arrangement, like in the picture below enter image description here

Also I have another plugin for Notepad++, "TextFX", I'm selecting all the text (Ctrl+A) and using Unwrap Text, like in the picture below enter image description here

After these actions I'm saving my xml file and it has 100KB (uploaded it here ). How can I do this action programatically in c# ?

Thanks in advance!

Upvotes: 2

Views: 3995

Answers (1)

oleksa
oleksa

Reputation: 4007

Are you asking about how to remove all space characters in the xml document? Please load it to the XmlDocument and read from OuterXml. You will get xml document in one line

var d = new Data();
var s = new XmlSerializer(d.GetType());
var sb = new StringBuilder();
var strStream = new StringWriter(sb);
s.Serialize(strStream, d);
Trace.WriteLine(sb.ToString());// formatted document

var xd = new XmlDocument();
xd.LoadXml(sb.ToString());
Trace.WriteLine(xd.OuterXml); // document without any surplus space character or linebreaks

Data is my custom class, please find it below. It does not contain any XML serialization control attributes. You can use any class instead of it.

public class Data
{
    public string BIC;
    public string Addressee;
    public string AccountHolder;
    public string Name;
    public string CityHeading;
    public string NationalCode;
    public bool MainBIC;
    public string TypeOfChange;
    public DateTime validFrom;
    public DateTime validTill;
    public int ParticipationType;
    public string Title { get; set; }
}

First trace produces well-formatted XML

<?xml version="1.0" encoding="utf-16"?>
<Data xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <MainBIC>false</MainBIC>
  <validFrom>0001-01-01T00:00:00</validFrom>
  <validTill>0001-01-01T00:00:00</validTill>
  <ParticipationType>0</ParticipationType>
</Data>

and second trace output is single line:

<?xml version="1.0" encoding="utf-16"?><Data xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><MainBIC>false</MainBIC><validFrom>0001-01-01T00:00:00</validFrom><validTill>0001-01-01T00:00:00</validTill><ParticipationType>0</ParticipationType></Data>

Upvotes: 5

Related Questions