我应该如何模板这个svg片段

本文关键字:svg 片段 何模板 我应该 | 更新日期: 2023-09-27 18:18:44

我需要制作一个模板,我将通过查找特殊标记并替换值来为下面的坐标插入新值。

我给出了一个SVG,下面是我需要更改的一小段内容,以便我可以模板/添加占位符,因为下面的值将是动态的。

<g id="Layer_1">
    <g id="shape1">
        <polygon class="poly1" points="6.7,105.4 -19.2,190.5 -20.4,123.5 1.6,143.6 "/>
    </g>
    <polygon id="shape2" class="poly1" points="-20.9,300.3 -37.7,103.3 -10.9,204.3 -0.1,233.6"/>
    <circle id="circleSolid" class="poly2" cx="6.7" cy="306.1" r="10.3"/>
    <g id="circleDashed">
        <circle class="poly3" cx="2.6" cy="304.7" r="10.3"/>
    </g>
    <text transform="matrix(1 0 0 1 -40.7301 191.3002)" class="poly1 poly3 poly4">Poly1</text>
    <text transform="matrix(1 0 0 1 -40.7302 200.3002)" class="poly1 poly3 poly4">Poly2</text>
</g>
<g id="Layer_7">
    <line id="newShape1" class="poly5" x1="-30.7" y1="170.7" x2="12.2" y2="171.7"/>
    <line id="newShape2" class="poly5" x1="-11.5" y1="160.7" x2="0.1" y2="200.3"/>
</g>>

我想知道我不能只是添加[x1]标签,例如下面的html标签模板。我应该只使用类来插入新的动态值吗?我应该如何去模板SVG绘图?

我将在c#中动态地插入新的值

我应该如何模板这个svg片段

您必须使用c#函数String.Format(),它将接受您的字符串并将字符串中的任何变量替换为out中的实际文本。

例如:

string name = "Scott";
string output = String.Format("Hello {0}", name); ///outputs Hello Scott

如果你想在SVG中生成circle元素,你需要这样写代码:

string cx = '6.7';
string cy = '306.1';
string r = 10.3';
string result = String.Format("<circle id="circleSolid" class="poly2" cx="{0}" cy="{1}" r="{2}"/>", cx, cy, r);

将输出如下内容:

<circle id="circleSolid" class="poly2" cx="6.7" cy="306.1" r="10.3"/>
  • 字符串。格式- MSDN