SvgSymbolOptions Class

Options used when converting an <svg> tag to an <symbol> tag or vice versa.

Definition

Namespace: DotMake.SvgSprite
Assembly: DotMake.SvgSprite (in DotMake.SvgSprite.dll) Version: 1.0.0
C#
public class SvgSymbolOptions
Inheritance
Object    SvgSymbolOptions

Example

C#
//Build an SVG sprite file from input SVG files with custom options

var svgDocument = new SvgDocument();
var svgSpriteBuilder = new SvgSpriteBuilder(svgDocument);
var svgSymbolOptions = new SvgSymbolOptions
{
    //These are default values for options
    AttributesToPreserve = new[] { "id", "viewBox", "class", "style", "fill", "stroke", "opacity", "transform" },
    ElementsToPreserve = new[] { "*" },
    IdForMissing = "symbol",
    IdReplacementChar = '-',
    IdLowerCased = true
};

foreach (var file in Directory.EnumerateFiles(@"inputs\", "*.svg"))
{
    var svgDocumentToAdd = new SvgDocument(file);
    var symbolId = Path.GetFileNameWithoutExtension(file);

    svgSpriteBuilder.AddSymbol(svgDocumentToAdd, symbolId, svgSymbolOptions);
}

svgDocument.Save(@"sprite.svg");
C#
//Extract symbols from an SVG sprite file to individual SVG files with custom options

var svgDocument = new SvgDocument(@"sprite.svg");
var svgSpriteBuilder = new SvgSpriteBuilder(svgDocument);
var svgSymbolOptions = new SvgSymbolOptions
{
    //These are default values for options
    AttributesToPreserve = new[] { "id", "viewBox", "class", "style", "fill", "stroke", "opacity", "transform" },
    ElementsToPreserve = new[] { "*" },
    IdForMissing = "symbol",
    IdReplacementChar = '-',
    IdLowerCased = true
};
var outputDirectory = @"outputs\";

foreach (var symbolId in svgSpriteBuilder.GetSymbolIds())
{
    var svgDocumentToExtract = svgSpriteBuilder.ExtractSymbol(symbolId, svgSymbolOptions);
    var svgFile = Path.Combine(outputDirectory, Path.ChangeExtension(symbolId, ".svg"));

    Directory.CreateDirectory(outputDirectory);
    svgDocumentToExtract.Save(svgFile);
}

Constructors

SvgSymbolOptions Initializes a new instance of the SvgSymbolOptions class.

Properties

AttributesToDiscard Gets or sets the attributes to discard when converting <svg> tag to <symbol> tag or vice versa.

Wildcards can be used: * - Zero or more characters, ? - Exactly one character.

Default value is null which means preserve all attributes set in AttributesToPreserve.

Attributes that should be usually preserved on <symbol> tag:

  • viewBox — must be preserved on <symbol> to define coordinate system
  • id — required for referencing the symbol later
  • fill, stroke, opacity, transform — if used for styling
  • class, style — if CSS is applied
  • width and height — optional; usually omitted in sprites

AttributesToPreserve Gets or sets the attributes to preserve when converting <svg> tag to <symbol> tag or vice versa.

Wildcards can be used: * - Zero or more characters, ? - Exactly one character.

Default value is { "id", "viewBox", "class", "style", "fill", "stroke", "opacity", "transform" }.

Attributes that should be usually preserved on <symbol> tag:

  • viewBox — must be preserved on <symbol> to define coordinate system
  • id — required for referencing the symbol later
  • fill, stroke, opacity, transform — if used for styling
  • class, style — if CSS is applied
  • width and height — optional; usually omitted in sprites

CommentsPreserved Gets or sets a value indicating whether to preserve comments when converting <svg> tag to <symbol> tag or vice versa.

Default value is .

Default Gets the default instance of the SvgSymbolOptions class.
ElementsToDiscard Gets or sets the elements to discard when converting <svg> tag to <symbol> tag or vice versa.

Wildcards can be used: * - Zero or more characters, ? - Exactly one character.

Default value is null which means preserve all elements set in ElementsToPreserve.

Elements that should be usually preserved inside <symbol> tag:

  • <path> — most common for icons
  • <circle>, <rect>, <line>, <polygon>, <polyline> — basic shapes
  • <g> — groups of elements (preserve if used for structure or styling)
  • <text> — if your icon includes text
  • <use> — if referencing other symbols
  • <defs> — only if it contains reusable elements like gradients or filters used inside the symbol

ElementsToPreserve Gets or sets the attributes to preserve when converting <svg> tag to <symbol> tag or vice versa.

Wildcards can be used: * - Zero or more characters, ? - Exactly one character.

Default value is { "*" }.

Elements that should be usually preserved inside <symbol> tag:

  • <path> — most common for icons
  • <circle>, <rect>, <line>, <polygon>, <polyline> — basic shapes
  • <g> — groups of elements (preserve if used for structure or styling)
  • <text> — if your icon includes text
  • <use> — if referencing other symbols
  • <defs> — only if it contains reusable elements like gradients or filters used inside the symbol

IdForMissing Gets or sets the value to use when id attribute is missing or is not specified when converting <svg> tag to <symbol> tag or vice versa.

When there are duplicates, this value will be incremented like "symbol-2", "symbol-3" etc.

Default value is "symbol".

IdLowerCased Gets or sets a value indicating whether to convert the id attribute to lowercase when converting <svg> tag to <symbol> tag or vice versa.

Default value is .

IdPrefix Gets or sets the prefix to add to the id attribute when converting <svg> tag to <symbol> tag or vice versa.

Default value is null which means not to add a prefix.

IdReplacementChar Gets or sets the replacement character when sanitizing the id attribute when converting <svg> tag to <symbol> tag or vice versa.

Default value is '-'. Allowed values are '-', '_', '.'. Setting any other value will disable sanitizing.

The disallowed characters will be replaced with IdReplacementChar if it was set.
SVG uses XML rules for id attributes, so the allowed characters are:

  1. Start Character
    • Must begin with a letter (A–Z, a–z) or underscore (_)
    • Cannot start with a number or hyphen (-)
  2. Subsequent Characters
    • Letters (A–Z, a–z)
    • Digits (0–9)
    • Underscore (_)
    • Hyphen (-)
    • Period (.)

ViewBoxOverride Gets or sets the override value to use for all viewBox attributes, e.g. "0 0 24 24", when converting <svg> tag to <symbol> tag or vice versa.

Default value is .

See Also