You can use XmlReader
. You call MoveToContent
to move to the first element in the document, then the XmlReader
instance will refer to the svg
tag (or non-svg
if it's a normal XML file):
using System.Xml;
{
var xml = """
<svg width="400" height="110">
<rect width="300" height="100" style="fill:rgb(0,0,255);stroke-width:3;stroke:rgb(0,0,0)" />
Sorry, your browser does not support inline SVG.
</svg>
""";
using var xmlReader = XmlReader.Create(new StringReader(xml));
xmlReader.MoveToContent();
if (xmlReader.Name.Equals("svg", StringComparison.OrdinalIgnoreCase)) {
Console.WriteLine("It's an SVG!");
} else {
Console.WriteLine("It is NOT an SVG!");
}
}
{
var xml = """
<?xml version="1.0" encoding="UTF-8"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
""";
using var xmlReader = XmlReader.Create(new StringReader(xml));
xmlReader.MoveToContent();
if (xmlReader.Name.Equals("svg", StringComparison.OrdinalIgnoreCase)) {
Console.WriteLine("It's an SVG!");
} else {
Console.WriteLine("It is NOT an SVG!");
}
}
If you're reading from a file like your question suggests then you can just pass the filename to XmlReader.Create
instead of the StringReader
.