Get XML string back from SvgDocument class

Oct 18, 2011 at 6:51 PM

as it is possible to add svg elements to the DOM on the fly in c#, i wonder if its possible to get the XML string back?

i have seen two write methods in the SvgDocument class. has someone implemented them yet (or partially)? how hard would it be to write them and how would the strategy be?

if i can do it with a reasonable amount of work, i would be grad to contribute it back.


Oct 18, 2011 at 11:27 PM

Hey tebjan,

I don't think there is any support in the current release, but the source code does have some support for it.

The Write methods on SvgDocument doesn't do anything, but if you create an instance of XmlWriter you should be able to call the WriteElement method.

Now, if this does work, there will still be some issues. Essentially for each property that is marked as [SvgAttribute] the TypeConverter for is used to convert the value to a string (which is assumed to be the SVG string). This means that types that don't have a TypeConverter defined won't be written properly. It should be pretty easy to fill in any missing TypeConverters though. Hopefully that would be the only work left to do, but I'm not sure of that.

Let me know how you go with this or if there is something that doesn't make sense.


Oct 19, 2011 at 5:59 PM

hello dave,

would be nice to have a short example (pseudo code), how the XmlWriter can be used together with the SvgAttributes. how the a class with SvgAttributes can transformed into the xml tag...

thanks for the help!

Oct 19, 2011 at 11:38 PM

Code similar to the following would write some XML (in a console program):


    class Program
        static void Main(string[] args)
            var document = new SvgDocument
                Width = 50,
                Height = 50

            document.Children.Add(new SvgCircle
                    Radius = 20,
                    Fill = new SvgColourServer(Color.Red),
                    Stroke = new SvgColourServer(Color.Black),
                    StrokeWidth = 2

            var xmlWriter = new XmlTextWriter(Console.Out);


However, it's NOT correct (sorry!):


<svg d1p1:width="50" d1p1:height="50" d1p1:viewBox="0, 0, 0, 0" xmlns:d1p1="">
  <circle d1p1:cx="none" d1p1:cy="none" d1p1:r="20" d1p1:clip-rule="NonZero" d1p1:visibility="True" ... />



The most immediate problem is that a dodgy namespace is being used for the attributes (d1p1). I suspect this will be a relatively simple (but not necessarily easy) fix in SvgElement.cs.WriteAttributes & other Write* methods. At first glance it appears that the problem is related to the SVG namespace not being written out correctly first. Overriding WriteAttributes in SvgDocument.cs or SvgFragment.cs and writing the XMLNS first could be the correct fix for this.

The second problem is that all attributes are being written out regardless of whether they have been modified. This shouldn't break anything because the default values are all correct according to the SVG spec, but it will create very verbose files.

As to the TypeConverter at work - it's quite easy to spot the issue here in the clip-rule attribute. The valid value would be "non-zero" (I think) but the SvgClipRule enum has no TypeConverter so the enum value .ToString() method is used to get a string value (NonZero is how it's represented in code). To fix this issue you'd just need to create a simple TypeConverter for SvgClipRule that overrides the CanConvertTo method to return true for types of string and then override the ConvertTo method to return the correct values. You can see examples of how this is done in SvgUnitConverter.cs and SvgTransformConverter.cs (and see SvgUnit to see how the TypeConverter is registered at the top of the class).

I don't think there is much work left to do here, but it's obviously not in a working state.


Oct 20, 2011 at 1:06 AM

thanks for the help! will work on it right away.. if i can fix all the issues i'll report back here.

Oct 26, 2011 at 12:20 AM

hey, just a short note. i have very promising results.. but still working on it.

how should i send you the code? as a patch upload or something?

Oct 26, 2011 at 4:17 AM

I think a Codeplex patch should be alright.

If you have trouble with that, perhaps create an Issue with the zipped up code attached. 

Nov 11, 2011 at 3:36 PM

alright, its getting bigger.. besides some type converters, i did many more bug fixes regarding viewbox, text element and other small things here and there. we decided to import the svn into a private repository on github to have my fixes and additions version controlled. i will send you a big patch in the next weeks. if you don't want to include this patch into the public svn here, we would like to ask you, if we could host some kind of 'fork' on github?

vvvv is a visual programming environment for realtime media.

but i hope you will like what i did so far anyways... 

Jan 12, 2012 at 7:06 PM

Tebjan, we are looking to do exactly the same thing as what you describe above. We have just started using svg in our application and we need the ability to export the lot to an svg file that can be rendered by the browser. This SVG Rendering engine seems perfect apart from the missing functionality as stated above.

Rather than re-inventing the wheel and making all these changes ourselves, is the work you have done available in the codeplex codebase as a patch? Where can I find it? (I cannot see the vvvv organisation on github).



Jan 21, 2012 at 12:49 AM
Edited Feb 16, 2012 at 10:10 PM


i have sent you a message with contact information over codeplex. there is no patch yet, but i can give you a .dll to test the functionality. would also be a good way to find bugs. so feel free to contact me.


Apr 5, 2013 at 4:47 PM
hello tebjan,

can u send me dll too?

Jun 4, 2013 at 8:40 PM
Edited Jun 4, 2013 at 8:40 PM
reg4ltip wrote:
can u send me dll too?
we made a forl on github, just head over to: