Search code examples
c#xmllinqsvglinq-to-xml

Replace SVG innertext value with a href link


I am working with SVG file, that are same of XML. I need to replace InnerText of some elements with a href new element with its InnerText

An example of my element is

<text xmlns="http://www.w3.org/2000/svg" xml:space="preserve" fill="#ff0000" stroke-width="0" x="239.61" y="74.89" font-size="3.37" opacity="1.00" font-family="Arial Narrow">3070119100</text>

I need to replace 3070119100 InnerText to

<a link:href="http://www.google.it">3070119100</a>

I suppose I need to add a child with as InnerText the previous value.

I would like to work with LINQ XDocument but is appreciate also code with XMLDocument.

WHAT I AM ABLE TO DO: I was able working with XMLDocument to add child but not to clear innerText text element:

    XmlDocument doc = new XmlDocument();
    doc.Load(_SVG);
    XmlNodeList elementListText = doc.GetElementsByTagName("text");
    for (int i = 0; i < elementListText.Count; i++)
    {
        XmlElement a = doc.CreateElement("a");
        a.SetAttribute("xmlns", "http://www.w3.org/2000/svg");
        a.SetAttribute("xmlns:xlink", "http://www.w3.org/1999/xlink");
        a.SetAttribute("xlink:href", "http://www.google.it");
        a.InnerText = elementListText[i].InnerText;
        elementListText[i].AppendChild(a);
    }
    doc.Save(_SVG_TEXT);

In this way I got value twice, one not linked (previous) and one linked (href value), but I am not able to clear text InnerText.

Thanks!


Solution

  • Following uses Xml Linq

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Xml;
    using System.Xml.Linq;
    
    
    namespace ConsoleApp10
    {
        class Program
        {
            static void Main(string[] args)
            {
    
                string _SVG = @"<text xmlns=""http://www.w3.org/2000/svg"" xmlns:xlink=""http://www.w3.org/2000/svg"" xml:space=""preserve"" fill=""#ff0000"" stroke-width=""0"" x=""239.61"" y=""74.89"" font-size=""3.37"" opacity=""1.00"" font-family=""Arial Narrow"">3070119100</text>";
                XDocument doc = XDocument.Parse(_SVG);
    
                //to read from file
                //XDocument doc = XDocument.Load(filename);
                XNamespace ns = doc.Root.GetDefaultNamespace();
                XNamespace xlink = doc.Root.GetNamespaceOfPrefix("xlink");
    
                List<XElement> elementListText = doc.Descendants(ns + "text").ToList();
                foreach(XElement element in elementListText)
                {
                    string value = element.Value;
                    element.RemoveNodes();
    
                    XElement a = new XElement(ns + "a");
                    a.SetAttributeValue(ns + "xlink", "http://www.w3.org/1999/xlink");
                    a.SetAttributeValue(xlink + "href", "http://www.google.it");
                    a.SetValue(value);
                    element.Add(a);
                }
                //doc.Save(filename);
            }
    
        }
    
    }