我正在尝试使用 vb.net 制作.svg文件,我需要添加公司徽标。
我使用以下代码添加图像元素:
'Add logo
.WriteStartElement("image")
.WriteAttributeString("width", "100")
.WriteAttributeString("height", "100")
.WriteAttributeString("xlink", "href", "data:img/png;base64, string of company logo")
.WriteAttributeString("x", "200")
.WriteAttributeString("y", "200")
但是我最终在我的XML文件中得到了这个:
<image width="100" height="100" p4:xlink="data:img/png;base64, string of company logo" />
但我想以:
<image width="100" height="100" xlink:href="data:img/png;base64, string of company logo" />
我做错了什么?我需要在代码中更改什么才能在.svg文件中获得所需的结果?
1.首先,您需要导入命名空间(将namespace
更改为所需的命名空间(
Imports <xmlns:xlink="mynamespace">
2.VB.NET 具有独特的功能 - XML 文本。您可以轻松地将值嵌入到元素和属性中:
Dim height = 100
Dim width = 100
Dim href = "data:img/png;base64, string of company logo"
Dim xml = <image width=<%= width %> height=<%= height %> xlink:href=<%= href %>/>
3.当您需要 XML 的字符串表示形式时调用 ToString((:
Dim xmlString = xml.ToString()
通过更改此行解决了我的问题:
.WriteAttributeString("xlink", "href", "data:img/png;base64, string of company logo")
对此
.WriteAttributeString("xlink", "href", Nothing, "data:img/png;base64, string of company logo")
不知道为什么这有效,我之前的解决方案没有....
我正要像@JohnyL的答案一样建议XML文字,但硬编码可能更容易:
Dim xml = "<image width=""100"" height=""100"" xlink:href=""data:img/png;base64," &
stringOfCompanyLogo & """ />"
File.WriteAllText(filePath, xml)
请注意,xlink:
命名空间在 SVG 2
中已弃用https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/xlink:href