chameLEON
chameLEON

Reputation: 23

How to read xml code from svg extension svg

So i'm having problems reading some xml. I need to read the <path> attributes.

The SVG looks like this:

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"  "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg width="700pt" height="820pt" viewBox="60 25 500 600" 
  version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" >
  <g id="id1">
    <g id="id3">
      <g id="id3">


 <path clip-path="url(#SVG_CP_1)" fill="#000000" city="Amsterdam" />

</g>
</g>
</g>
</svg>

my code:

XDocument xdoc = XDocument.Load("D:/Users/me/Desktop/Website/files/test.svg");
            xdoc.Descendants("path").Select(p => new
            {
                city= p.Attribute("city").Value,

            }).ToList().ForEach(p =>
            {
                html.Append("City:  " + p.city+ "<br>");


            });

Now this code works perfectly when I exclude <svg width='700pt' etc... > from the svg file . But I need it in my file.

Upvotes: 2

Views: 1600

Answers (1)

har07
har07

Reputation: 89315

The root element svg contains default namespace declaration which URI is http://www.w3.org/2000/svg. Notice that all descendant elements without prefix implicitly inherit ancestor's default namespace.

Now to reference element in namespace, you can use combination of XNamespace and the target element's local name :

XDocument xdoc = ...;
XNamespace ns = "http://www.w3.org/2000/svg";
xdoc.Descendants(ns+"path").Select(p => new
{
    ....
})
....

Upvotes: 1

Related Questions