0

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.

Ceres
  • 3,524
  • 3
  • 18
  • 25
  • See [this question](http://stackoverflow.com/q/13613917/897326). The solution is to remove the xmlns attribute manually after adding the node. – Victor Zakharov Apr 12 '13 at 21:11

1 Answers1

1

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.

Martin Honnen
  • 160,499
  • 6
  • 90
  • 110
  • Thanks, I was hoping for a simple solution. It's also in .Net 4.0 which is what I need. – Ceres Apr 14 '13 at 13:38