【发布时间】:2012-02-29 15:35:55
【问题描述】:
我正在尝试使用以下行将 svg 字符串解析为 XElement:
XElement productSvg = XElement.Parse(svgString);
但我收到以下异常:XmlException - 'xlink' is an undeclared prefix 这是我的svg:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg id="svg62433" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 595.19 596.32" version="1.1" xmlns:cc="http://creativecommons.org/ns#" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:dc="http://purl.org/dc/elements/1.1/">
<g id="g7123">
<path id="path5588" d="m404.54,164.29c0,121.64-98.612,220.26-220.26,220.26-121.64,0-220.26-98.612-220.26-220.26,0-121.64,98.612-220.26,220.26-220.26,121.64,0,220.26,98.612,220.26,220.26z" transform="matrix(0.01922722,0.99981514,-0.99981514,0.01922722,536.42634,464.95177)" fill="none"/>
<text id="text5590" style="writing-mode:lr-tb;letter-spacing:0px;text-anchor:middle;word-spacing:0px;text-align:center;" font-weight="bold" font-size="64px" line-height="125%" font-stretch="normal" font-variant="normal" font-style="normal" font-family="'Swis721 Cn BT'">
<textPath id="textPath5598" xlink:href="#path5588" startOffset="50%">
<tspan id="contentTopText" style="writing-mode:lr-tb;text-anchor:middle;text-align:center;" font-weight="bold" font-size="64px" line-height="125%" font-stretch="normal" font-variant="normal" font-style="normal" font-family="'Swis721 Cn BT'">Text Here</tspan>
</textPath>
</text>
</g>
</svg>
有谁知道如何成功解析 .NET Xlinq 中包含命名空间的 xml 属性?
【问题讨论】:
-
好吧,无论是 LINQ to XML 还是任何其他 XML API,如果你想使用它,你需要以命名空间格式良好的 XML 开头,这意味着需要声明
xlink前缀例如xmlns:xlink="http://www.w3.org/1999/xlink",就像使用的任何其他命名空间前缀一样。如果你没有,你需要一个 HTML5 解析器,而不是 .NET 到目前为止 (4.0) 没有。 -
@MartinHonnen,在父svg标签中声明了xlink命名空间
-
@svick,很酷,对于那些遇到这个问题的人,我在这里找到了一个很好的答案:aspnetgotyou.blogspot.com/2010/06/…
-
我不明白如果声明了前缀,您如何获得未声明前缀的错误消息。所以听起来好像您发布的 XML 与您收到错误的 XML 不同。
-
不,我肯定得到了例外,否则不会打扰发布问题。我在下面找到的链接是一个很好的解决方案。请注意,我不是在这里解析实际的 svg xml 文档,它是一个 svg 字符串。
标签: .net xml svg linq-to-xml