BjarkeCK
BjarkeCK

Reputation: 5729

Change color of all elements on any svg file

I can easily add a fill on the element (<path fill="#FF0000">), but my problem is, that it is not always a path element, it can also be <rect>, <ellipse> and so on.

but what if i want to change the color of all the elements in the svg file? no matter what type it is?

My solutions:

I don't like any of these solutions... The first is not dynamic, and the other is not dynamic is dirty¨

Is there something smarter i can do here? can i define a global color or something? Or does svg.codeplex have some functionality that i can use (which i can't find)?

Upvotes: 2

Views: 5375

Answers (2)

Chuck Savage
Chuck Savage

Reputation: 11955

Have you tried XPath? (using System.Xml.XPath)

string newColor = "#123456";
XElement root = XElement.Load(file);
var colors = root.XPathSelectElements("//*[@fill]");
foreach(XElement node in colors)
    node.Attribute("fill").Value = newColor;

PS this is an answer to your first bullet.

Upvotes: 4

Robert Longson
Robert Longson

Reputation: 124189

Use css to set the colours. If you remove the fill styles from the elements themselves then you can just add

<style type="text/css">
  <![CDATA[
    * {
      fill: #FF0000;
    }
  ]]>
</style>

or alternatively in an external stylesheet and everything that accepts a fill colour will be red. You can manipulate the styles using document.styleSheets[1]

Upvotes: 3

Related Questions