Class SvgAttributeCollection
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
this[int]
Gets the attribute at an index, in file order.
public SvgAttribute this[int index] { get; }
Parameters
indexintThe index.
Property Value
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
namestringThe local name, such as
fill.
Property Value
Methods
Contains(string, string?)
Gets whether the element has an attribute.
public bool Contains(string name, string? namespaceUri = null)
Parameters
Returns
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
Returns
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
Returns
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
namestringThe local name, such as
x.valuedoubleThe number; it must be finite.
namespaceUristringThe namespace, or null for none.
prefixstringThe prefix a save writes for an attribute in a namespace, or null.
Exceptions
- ArgumentOutOfRangeException
valueis 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
namestringThe local name.
valuestringThe value, or null to remove the attribute.
namespaceUristringThe namespace, or null for none.
prefixstringThe 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)