简体   繁体   中英

Get attribute value from the SVG (XML) root element

I've successfully gotten attribute values from sub-elements in my SVG using an XML parser, but I'm having trouble getting the viewbox value from the same SVG.

Here's the top of the SVG. I'm trying to parse out "0 0 2491 2491" from the viewBox attribute of the svg element:

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<svg viewBox="0 0 2491 2491" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" stroke-linecap="round" stroke-linejoin="round" fill-rule="evenodd" xml:space="preserve">
<defs>
<clipPath id="clipId0">
<path d="M0,2491 2491,2491 2491,0 0,0 z" />
</clipPath>
</defs>
<g clip-path="url(#clipId0)" fill="none" stroke="rgb(100,100,100)" stroke-width="0.5" />

Some example code that yielded no results:

//from calling method
xmlParser.GetAttributeValueAtSubElement("svg", "viewBox")

//class variables
private readonly XNamespace _NameSpace = "http://www.w3.org/2000/svg"; 
private readonly XNamespace _NameSpace_xlink = "http://www.w3.org/1999/xlink";  

//class constructor
        public XMLParser(string filePath)
        {
            _FilePath = filePath;
            _XML_Doc = XDocument.Load(_FilePath);
            _XML_Elem = XElement.Load(_FilePath);
        }

//attempt 1 failed
    public string GetAttributeValueAtSubElement(string subElementName, string attributeName)
    {

        string rv = string.Empty;
        IEnumerable<XAttribute> attribs =
            from el in _XML_Elem.Descendants(_NameSpace + subElementName) 
            select el.Attribute(attributeName);


        foreach (XAttribute attrib in attribs)
        { rv = attrib.Value; }

        return rv;
    }

//attempt 2 failed
        public string GetAttributeValueAtSubElement(string subElementName, string attributeName)
        {

        string rv = string.Empty;

        IEnumerable<XAttribute> attribs =
           from el in _XML_Elem.Elements(_NameSpace + subElementName) 
            select el.Attribute(attributeName);

        foreach (XAttribute attrib in attribs)
        { rv = attrib.Value; }

        return rv;
    }

Simple. Viewbox is not a descendant but the root. :

            XDocument doc = XDocument.Load(FILENAME);
            XElement svg = doc.Root;

            string viewBox = (string)svg.Attribute("viewBox");

After not having any luck or getting any responses here, I successfully got the values I needed from the first path Element by using:

    public string GetAttributeValueAtSubElement()
    {

        string rv = string.Empty;

        IEnumerable<XAttribute> attribs =
        from el in _XML_Elem.Descendants(_NameSpace + "path")  also?
            select el.Attribute("d");

        if (attribs.Count() > 0 && attribs.First<XAttribute>().Value.Contains("M0,")
            && attribs.First<XAttribute>().Value.Contains("z"))
            rv = attribs.First<XAttribute>().Value; 

        return rv;
    }

Which returns "M0,2491 2491,2491 2491,0 0,0 z"

The first path has the same coordinates as the viewbox, so this should work for me.

EDIT: This worked, but after receiving the answer that I ultimately selected, I adjusted my code to go with the approach in that answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM