Class SvgElement
An element of an SVG document's editable tree: its name and namespace, attributes and content, like XDocument's elements. Unknown elements and foreign namespaces (Inkscape's, Illustrator's metadata) are kept, so a save writes them back.
public sealed class SvgElement : SvgContent, ICssElement
- Inheritance
-
SvgElement
- 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");
}
Remarks
Every change marks the document's render tree for rebuilding on the next draw. The tree is not safe for writes from several threads at once; a document that is not being edited can be drawn on many threads.
Constructors
SvgElement(string)
Initializes a new instance of the SvgElement class in the SVG namespace.
public SvgElement(string name)
Parameters
namestringThe local name, such as
rect.
SvgElement(string, string?, string?)
Initializes a new instance of the SvgElement class.
public SvgElement(string name, string? namespaceUri, string? prefix = null)
Parameters
namestringThe local name.
namespaceUristringThe namespace, or null for none.
prefixstringThe prefix a save writes, or null for the default namespace.
Properties
Attributes
Gets the attributes, in file order.
public SvgAttributeCollection Attributes { get; }
Property Value
Children
Gets the content, in file order: child elements, text, comments and processing instructions.
public SvgContentList Children { get; }
Property Value
Id
Gets or sets the id attribute; null when absent.
public string? Id { get; set; }
Property Value
IsSvg
Gets a value indicating whether this is an element in the SVG namespace.
public bool IsSvg { get; }
Property Value
Name
Gets the local name, such as rect.
public string Name { get; }
Property Value
Namespace
public string? Namespace { get; }
Property Value
Prefix
Gets the prefix a save writes, or null when the element is in the default namespace.
public string? Prefix { get; }
Property Value
Text
Gets or sets the text of this element and its descendants, joined in order. Setting replaces all content with one text item.
public string Text { get; set; }
Property Value
Methods
Clone()
Makes a deep copy without a parent: an element is copied with its attributes and all its content.
public override SvgContent Clone()
Returns
- SvgContent
The copy.
Descendants()
Gets every element below this one, depth first in file order, not including this one.
public IEnumerable<SvgElement> Descendants()
Returns
- IEnumerable<SvgElement>
The descendant elements.
Elements()
Gets the child elements, in order, skipping text and comments.
public IEnumerable<SvgElement> Elements()
Returns
- IEnumerable<SvgElement>
The child elements.
Elements(string)
Gets the child elements with a local name, in the SVG namespace.
public IEnumerable<SvgElement> Elements(string name)
Parameters
namestringThe local name, such as
stop.
Returns
- IEnumerable<SvgElement>
The matching child elements.
ToString()
Returns a string that represents the current object.
public override string ToString()
Returns
- string
A string that represents the current object.