2

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!

Alex
  • 8,908
  • 28
  • 103
  • 157
  • Can you provide here, in question, at least small part of both xml file, so we can understand what do you need – Rocketq Feb 12 '14 at 10:23

1 Answers1

5

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>
oleksa
  • 3,688
  • 1
  • 29
  • 54
  • I need only document without any surplus space character or linebreaks I use from your code var xd = new XmlDocument(); xd.LoadXml(strBuilder.ToString()); context.Response.Write(xd.OuterXml); and generated xml have the same size – Alex Feb 12 '14 at 11:45
  • @Alex I've added Data class definition and trace output. Do you have nodes like [XmlProcessingInstruction](http://stackoverflow.com/questions/203528/what-is-the-simplest-way-to-get-indented-xml-with-line-breaks-from-xmldocument) in your XML ? To get human-friendly XML from XmlDocument you have to use special writer formating. I'm using .NET 4 if it matters. However XmlDocument produces the same output in .NET 2 afair. I suppose that you have to use JSON formatter (if client is written by you too) It will reduce serialized string size at 1/3 comparing to xml format. – oleksa Feb 12 '14 at 14:11