0

Take this SVG:

<svg width="100%" height="100%" viewBox="0 0 512 512" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xml:space="preserve" xmlns:serif="http://www.serif.com/" style="fill-rule:evenodd;clip-rule:evenodd;stroke-linejoin:round;stroke-miterlimit:2;">
<rect id="Image" x="0" y="0" width="512" height="512" style="fill:none;" />


<g id="Image1" serif:id="Image">
<g transform="matrix(0.564048,0,0,0.564048,5.98262,470.315)">
<g transform="matrix(591.224,0,0,591.224,0,0)">
<path d="M0.644,-0.828C0.608,-0.891 0.517,-0.891 0.481,-0.828L0.013,-0.016C-0.023,0.047 0.022,0.125 0.094,0.125L1.031,0.125C1.103,0.125 1.148,0.047 1.112,-0.016L0.644,-0.828Z" style="fill-rule: nonzero" />
            </g>
        </g>
        
<g transform="matrix(-0.564048,-6.9076e-17,6.9076e-17,-0.564048,508.145,41.6849)">
<g transform="matrix(591.224,0,0,591.224,0,0)">
<path d="M0.644,-0.828C0.608,-0.891 0.517,-0.891 0.481,-0.828L0.013,-0.016C-0.023,0.047 0.022,0.125 0.094,0.125L1.031,0.125C1.103,0.125 1.148,0.047 1.112,-0.016L0.644,-0.828Z" style="fill: rgb(158, 158, 158); fill-rule: nonzero" />
            </g>
        </g>
    </g>
</svg>

I would like to "butify" it, so it looks like this (remove empty lines and indent):

<svg width="100%" height="100%" viewBox="0 0 512 512" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xml:space="preserve" xmlns:serif="http://www.serif.com/" style="fill-rule:evenodd;clip-rule:evenodd;stroke-linejoin:round;stroke-miterlimit:2;">
    <rect id="Image" x="0" y="0" width="512" height="512" style="fill:none;" />
    <g id="Image1" serif:id="Image">
        <g transform="matrix(0.564048,0,0,0.564048,5.98262,470.315)">
            <g transform="matrix(591.224,0,0,591.224,0,0)">
                <path d="M0.644,-0.828C0.608,-0.891 0.517,-0.891 0.481,-0.828L0.013,-0.016C-0.023,0.047 0.022,0.125 0.094,0.125L1.031,0.125C1.103,0.125 1.148,0.047 1.112,-0.016L0.644,-0.828Z" style="fill-rule: nonzero" />
            </g>
        </g>
        <g transform="matrix(-0.564048,-6.9076e-17,6.9076e-17,-0.564048,508.145,41.6849)">
            <g transform="matrix(591.224,0,0,591.224,0,0)">
                <path d="M0.644,-0.828C0.608,-0.891 0.517,-0.891 0.481,-0.828L0.013,-0.016C-0.023,0.047 0.022,0.125 0.094,0.125L1.031,0.125C1.103,0.125 1.148,0.047 1.112,-0.016L0.644,-0.828Z" style="fill: rgb(158, 158, 158); fill-rule: nonzero" />
            </g>
        </g>
    </g>
</svg>

I've tried lots of code like this:

public static string PrintXML(string xml)
{
    string result = "";

    MemoryStream mStream = new MemoryStream();
    XmlTextWriter writer = new XmlTextWriter(mStream, Encoding.Unicode);
    XmlDocument document = new XmlDocument();

    try
    {
        // Load the XmlDocument with the XML.
        document.LoadXml(xml);

        writer.Formatting = Formatting.Indented;

        // Write the XML into a formatting XmlTextWriter
        document.WriteContentTo(writer);
        writer.Flush();
        mStream.Flush();

        // Have to rewind the MemoryStream in order to read
        // its contents.
        mStream.Position = 0;

        // Read MemoryStream contents into a StreamReader.
        StreamReader sReader = new StreamReader(mStream);

        // Extract the text from the StreamReader.
        string formattedXml = sReader.ReadToEnd();

        result = formattedXml;
    }
    catch (XmlException)
    {
        // Handle the exception
    }

    mStream.Close();
    writer.Close();

    return result;
}

Format XML string to print friendly XML string

... but I can't beautify the svg.

Any suggestions?

UPDATE:

Using the PrintXML code does not indent the result. The output is the same as the input.

MojoDK
  • 4,410
  • 10
  • 42
  • 80
  • 1
    `I can't beautify the svg.` what was the output? Besides, *why* do you want to beautify an SVG? It's not human-readable, it's an image format. People typically want to *remove* whitespace to reduce the size of the image. Your desired output contains extra whitespace instead – Panagiotis Kanavos Jul 29 '20 at 07:35
  • @PanagiotisKanavos ... output is the same as the input. I work a lot in the svg code, so I wan't to beautify it to make it easier to work in. When I'm done, then I will remove whitespaces etc. – MojoDK Jul 29 '20 at 07:38
  • `remove empty lines and indent` that's minification, the opposite of beautification. [This answer](https://stackoverflow.com/questions/4724940/is-there-any-way-to-save-an-xmldocument-without-indentation-and-line-returns) shows how to do it with just three lines if you use XmlDocument. XDocument can [also do this by passing](https://stackoverflow.com/questions/4724940/is-there-any-way-to-save-an-xmldocument-without-indentation-and-line-returns) – Panagiotis Kanavos Jul 29 '20 at 07:38
  • You just copied the other answer's code, without understanding what it does. First of all, what class do you use? XmlDocument? XDocument? Both have their own way to format output and preserve or *not* preserve whitespace. There's no need to go through a memory stream, unless you want to produce a string instead of save a file – Panagiotis Kanavos Jul 29 '20 at 07:40
  • @PanagiotisKanavos ... I'm not looking for "minification" - I need to beautify the svg. :) – MojoDK Jul 29 '20 at 07:40
  • I'm using XmlDocument. I would expect the "writer.Formatting = Formatting.Indented;" to indent the code, but it doesn't. – MojoDK Jul 29 '20 at 07:44
  • @PanagiotisKanavos despite the user's goal, SVG is useful just because is easy to manipulate, both via software and via text editor. If you mean to create/adjust a SVG image via editor, a tidy XML is *very* useful! – Mario Vernari Jul 29 '20 at 07:46
  • Found the problem. My svg code has the tag xml:space=""preserve"" which makes XmlTextWriter ignore indenting. – MojoDK Jul 29 '20 at 08:03
  • Use XmlWriter : XmlWriterSettings settings = new XmlWriterSettings(); settings.Ident = true; XmlWriter writer = new XmlWriter.Create(mStream, settings); – jdweng Jul 29 '20 at 09:28

1 Answers1

1

I fund the problem.

My SVG code has this tag xml:space="preserve" which makes XmlTextWriter ignore indenting etc.

MojoDK
  • 4,410
  • 10
  • 42
  • 80