How to modify generated SVG-XML-Code?

Hi,

is it possible to modify the xml code generated by the "GenerateBody" method from the gosvggenerator?
For example the GenerateBody(gotext) of a gotext element generates the following svg code:

8
What I want to do is to modify this XML code. The GenerateBody unfortunately no faunction that passed back an XMLNode or XMLElement that can be modified afterwards. Is there a way to access the generated xml code of the Generate[...] methods?
In my case I want to add an animation to the exported text like this:

8

Cheers,
Michael

You can replace the standard SVG generator for GoText by defining your own GoSvgGenerator:

public class CustomTextGenerator : GoSvgGenerator { public override void GenerateBody(object obj) { base.GenerateBody(obj); WriteStartElement("animateColor"); WriteAttrVal("attributeName", "fill"); WriteAttrVal("attributeType", "CSS"); WriteAttrVal("begin", "1s"); WriteAttrVal("dur", "5s"); WriteAttrVal("from", "#FFFFFF"); WriteAttrVal("to", "#F00"); WriteAttrVal("fill", "freeze"); WriteEndElement(); } }
Install this by doing:

[code] GoSvgWriter w = new GoSvgWriter();
CustomTextGenerator ctg = new CustomTextGenerator();
ctg.InheritsFromTransformer = w.GetTransformer(typeof(GoText));
w.SetTransformer(typeof(GoText), ctg);

XmlDocument svgdoc = w.Generate();
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
XmlWriter svgwriter = XmlWriter.Create(@"C:\temp\text.svg", settings);
svgdoc.WriteContentTo(svgwriter);
svgwriter.Close();[/code]

The InheritsFromTransformer property causes the call to base.GenerateBody to get the standard GoText generator to produce what it normally does.

However, this doesn’t generate exactly what you want. It produces:

<g> <text font-size="24pt" fill="rgb(0,191,255)"> <tspan x="128" y="158" textLength="154.32811">Some Text</tspan> </text> <animateColor attributeName="fill" attributeType="CSS" begin="1s" dur="5s" from="#FFFFFF" to="#F00" fill="freeze" /> </g>
Since you want to annotate XML deep inside what a generator normally produces, you cannot use the above technique to add some attributes (before the call to base.GenerateBody) or some elements to what is normally produced by the GoText SVG generator.

So your interest in getting to the XmlElement is going in the right direction. If you generate an XmlDocument instead of writing an XML stream, you can modify the XML DOM in memory.

Hence the start of the generator you want is:

// this only works for generating an XmlDocument public class CustomTextGenerator : GoSvgGenerator { public override void GenerateBody(object obj) { base.GenerateBody(obj); XmlElement elt = this.Writer.WriterElement; // ELT is now the <G> element XmlElement textelt = elt.ChildNodes[0] as XmlElement; // this should be the <TEXT> element } }
Sorry I don’t have the time to finish this for you. You do need to know System.Xml, of course.

One of the benefits of GoXml is that you don’t need to know System.Xml. Another benefit is that it works with both streams and with Xml DOM.

Thank you for your help! Perfect!

This line was the missing link:
XmlElement elt = this.Writer.WriterElement;
Cheers,
Michael