Table of Contents

Class SvgAttributeCollection

Namespace
Egami.Svg
Assembly
Egami.Svg.dll

An element's attributes, in file order. Setting, adding and removing mark the document's render tree for rebuilding.

public sealed class SvgAttributeCollection : IReadOnlyList<SvgAttribute>, IReadOnlyCollection<SvgAttribute>, IEnumerable<SvgAttribute>, IEnumerable
Inheritance
SvgAttributeCollection
Implements
Inherited Members

Examples

SvgDocument document = SvgDocument.Parse(Icon);

// Elements, attributes and text are an editable tree, like XDocument's.
SvgElement body = document.FindById("body")!;
body.Attributes["fill"] = "#c33";
body.Attributes.Set("href", "#other", SvgNamespaces.XLink, "xlink");

SvgElement dot = new("circle");
dot.Attributes["cx"] = "12";
dot.Attributes["cy"] = "12";
dot.Attributes["r"] = "3";
document.Root.Children.Add(dot);
document.Root.Children.Add(new SvgComment(" added in code "));

foreach (SvgElement element in document.Root.Elements())
{
    Console.WriteLine($"{element.Name}: {element.Attributes.Count} attributes");
}

Properties

Count

Gets the number of attributes.

public int Count { get; }

Property Value

int

this[int]

Gets the attribute at an index, in file order.

public SvgAttribute this[int index] { get; }

Parameters

index int

The index.

Property Value

SvgAttribute

this[string]

Gets or sets the value of an attribute in no namespace; null when absent, and setting null removes it.

public string? this[string name] { get; set; }

Parameters

name string

The local name, such as fill.

Property Value

string

Methods

Contains(string, string?)

Gets whether the element has an attribute.

public bool Contains(string name, string? namespaceUri = null)

Parameters

name string

The local name.

namespaceUri string

The namespace, or null for none.

Returns

bool

true if it does.

Get(string, string?)

Gets the value of an attribute, or null when the element has none of that name.

public string? Get(string name, string? namespaceUri = null)

Parameters

name string

The local name.

namespaceUri string

The namespace, or null for none.

Returns

string

The value, or null.

GetEnumerator()

Returns an enumerator that iterates through the collection.

public IEnumerator<SvgAttribute> GetEnumerator()

Returns

IEnumerator<SvgAttribute>

An enumerator that can be used to iterate through the collection.

Remove(string, string?)

Removes an attribute.

public bool Remove(string name, string? namespaceUri = null)

Parameters

name string

The local name.

namespaceUri string

The namespace, or null for none.

Returns

bool

true if the element had the attribute.

Set(string, double, string?, string?)

Sets an attribute to a number. It reads back in full precision; a save writes it with Precision decimals.

public void Set(string name, double value, string? namespaceUri = null, string? prefix = null)

Parameters

name string

The local name, such as x.

value double

The number; it must be finite.

namespaceUri string

The namespace, or null for none.

prefix string

The prefix a save writes for an attribute in a namespace, or null.

Exceptions

ArgumentOutOfRangeException

value is not finite.

Set(string, string?, string?, string?)

Sets an attribute, replacing one of the same name and namespace in place, or adding it at the end; null removes it.

public void Set(string name, string? value, string? namespaceUri = null, string? prefix = null)

Parameters

name string

The local name.

value string

The value, or null to remove the attribute.

namespaceUri string

The namespace, or null for none.

prefix string

The prefix a save writes for an attribute in a namespace, or null to keep or pick one.

SetPath(string, Path)

Sets an attribute, usually d, to path data. It reads back as SVG path data; a save writes it with Precision decimals.

public void SetPath(string name, Path path)

Parameters

name string

The local name, such as d.

path Path

The path.