Cannot cast an object of 'Aspose.Svg.Dom.Element' to type 'Aspose.Svg.SVGCircleElement'

       // Set SVG Namespace Url
        string SvgNamespace = "";

        string documentPath = Path.Combine(ConstValue.SvgPDAResourceRootDir, "demo2.svg");

        using (var document = new SVGDocument(documentPath))
            // Get root svg element of the document
            var svgElement = document.RootElement;

            // Create a circle element and set attributes values
            var circleElement = (SVGCircleElement)document.CreateElementNS(SvgNamespace, "circle");
            circleElement.Cx.BaseVal.Value = 100F;
            circleElement.Cy.BaseVal.Value = 100F;
            circleElement.R.BaseVal.Value = 50F;
            circleElement.SetAttribute("fill", "Salmon");

            // Add the circle element as the first child to svg element
            svgElement.InsertBefore(circleElement, svgElement.FirstChild);

            // Work with the document here...
            // Add a polyline and change stroke attributes for all circle and ellipse elements (see later)

            document.Save(Path.Combine(ConstValue.SvgPDAResourceRootDir, "demo2.svg"));

The above code encountered the following error during execution:
Cannot cast an object of ‘Aspose.Svg.Dom.Element’ to type ‘Aspose.Svg.SVGCircleElement’


Would you please share your sample SVG in .zip format with us as well? We will test the scenario in our environment and address it accordingly.

Same here with an empty new created document. Tested it with .NET472 and .NET Core 6
and different versions of Aspose.Svg

            var documentContent = "<svg xmlns=\"\"></svg>";
            using (var document = new SVGDocument(documentContent, "."))

                var gElement = (SVGGElement)document.CreateElementNS("", "g");

Version 23.5.0


We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): SVGNET-231

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.


The problem is caused by the incorrect SVG namespace being used when creating the element. In this case, you are using the HTTPS version of the namespace (“”), while the standard namespace for SVG documents is “SVG namespace” (notice the HTTP, not HTTPS).

To avoid this error, you should use the namespace that is already associated with the SVG document, as it might be slightly different from the one you are using. This can be achieved with the following line of code:

string SvgNamespace = document.RootElement.NamespaceURI;

Then, the SvgNamespace should be used in the CreateElementNS method:

var circleElement = (SVGCircleElement)document.CreateElementNS(SvgNamespace, "circle");

This way, the namespace will be consistent with the rest of the SVG document, and the InvalidCastException should no longer occur.