Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active February 23, 2021 09:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-com-gists/32cf16c0f8fd21b90ab8f787c6af0395 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/32cf16c0f8fd21b90ab8f787c6af0395 to your computer and use it in GitHub Desktop.
Inspect, Extract, or Navigate SVG Image Elements Programmatically using C#
//Load a document from a file
string documentPath = Path.Combine(DataDir, "shapes.svg");
using (var document = new SVGDocument(documentPath))
{
// Get the root svg element of the document
var svg = document.DocumentElement;
// Find the first child element with a given tag name
var g = svg.GetElementsByTagName("g").First() as SVGGElement;
var rect = g.FirstElementChild as SVGRectElement;
Console.WriteLine("Height: {0}", rect.Height);// 90
Console.WriteLine("Width: {0}", rect.Width); // 100
}
// Load a document
string documentPath = Path.Combine(DataDir, "shapes.svg");
using (var document = new SVGDocument(documentPath))
{
var element = document.DocumentElement;
Console.WriteLine(element.TagName); // svg
element = element.LastElementChild;
Console.WriteLine(element.TagName); // g
element = element.FirstElementChild;
Console.WriteLine(element.TagName); // rect
}
using Aspose.Svg;
using System.IO;
using Aspose.Svg.Dom;
using Aspose.Svg.Collections;
...
using (var document = new SVGDocument(Path.Combine(DataDir, "shapes.svg")))
{
// Evaluate XPath expression
var xpathResult = document.Evaluate("//rect[@x='120']", document, null, (Dom.XPath.XPathResultType)XPathResultType.Any, null);
// Get the next evaluated node
Console.WriteLine((xpathResult.IterateNext() as Element)?.OuterHTML);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment