Whixh XML Parser to use?

Peter Volz 1,295 Reputation points
2023-06-10T17:35:34.8766667+00:00

Hello all

According to this post:

https://stackoverflow.com/questions/15136264/how-can-i-say-a-file-is-svg-without-using-a-magic-number

To detect if a file is xml or svg , check for initial <?xml will not work, sometimes, xml documents won't start with that preamble, best way is to use a real XML parser to test that the file is well-formed XML that contains the svg top-level element, however, the given code is Phyton which I don't understand at all:


Anyone knows the best .NET xml parser to use and how to achieve the similar function?

Thanks indeed :)

Developer technologies VB
Developer technologies C#
{count} votes

3 answers

Sort by: Most helpful
  1. P a u l 10,761 Reputation points
    2023-06-11T13:30:04.2933333+00:00

    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.


  2. Peter Volz 1,295 Reputation points
    2023-06-14T17:21:19.97+00:00

    Hello, thanks, seems not working on these SVG files:

    https://dev.w3.org/SVG/tools/svgweb/samples/svg-files/anim1.svg

    0 comments No comments

  3. Dewayne Basnett 1,381 Reputation points
    2023-06-15T14:39:02.76+00:00

    XElement use

            Dim doc As XElement = <svg width="500" height="500">
                                      <rect id="rec" x="300" y="100" width="300" height="100" style="fill:lime">
                                          <animate attributeName="x" attributeType="XML" begin="0s" dur="6s" fill="freeze" from="300" to="0"/>
                                          <animate attributeName="y" attributeType="XML" begin="0s" dur="6s" fill="freeze" from="100" to="0"/>
                                          <animate attributeName="width" attributeType="XML" begin="0s" dur="6s" fill="freeze" from="300" to="800"/>
                                          <animate attributeName="height" attributeType="XML" begin="0s" dur="6s" fill="freeze" from="100" to="300"/>
                                          <animate attributeName="fill" attributeType="CSS" from="lime" to="red" begin="2s" dur="4s" fill="freeze"/>
                                      </rect>
                                  </svg>
    
    
    
            Dim s As String = doc.ToString
            Stop 'look at s, no <?xml version="1.0" encoding="utf-8"?>
            IO.File.WriteAllText("C:\Users\foo\Desktop\svgTest.svg", s)
    
            doc = Nothing
            Stop
            doc = XElement.Load("C:\Users\foo\Desktop\svgTest.svg")
            Stop
    
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.