MojoDK
MojoDK

Reputation: 4528

Can't "beautify" SVG

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.

Upvotes: 0

Views: 638

Answers (1)

MojoDK
MojoDK

Reputation: 4528

I fund the problem.

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

Upvotes: 1

Related Questions