Question

I feel like this should be easier than it is but I can't seem to get it straight. Here is my test case.

    Imports <xmlns="http://www.w3.org/2000/svg">

    Public Sub Test()

        Dim doc As XDocument = XDocument.Load("input.svg")
        Dim svg As XElement = doc.Elements.First

        svg.Add(<text>
                    <tspan>Some Text</tspan>
                </text>)

        svg.Save("output.svg")

    End Sub

the input xml

<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns="http://www.w3.org/2000/svg">
    <g  id="layer4" >
    </g>
</svg>

What I get is the following for output,

<?xml version="1.0" encoding="utf-8"?>
<svg xmlns="http://www.w3.org/2000/svg">
  <g id="layer4"></g>
  <text xmlns="http://www.w3.org/2000/svg">
    <tspan>Some Text</tspan>
  </text>
</svg>

Why doesn't the new text element see that it's using the default namespace and leave off the xmlns? If I leave off the Imports <xmlns="http://www.w3.org/2000/svg"> statement, I get an empty namespace xmlns="" on the text element.

Was it helpful?

Solution

In recent versions of .NET (4.5, maybe 4.0 too) you can save with

doc.Save("output.svg", SaveOptions.OmitDuplicatNamespaces)

see http://msdn.microsoft.com/en-us/library/bb551426.aspx.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top