gpt4 book ai didi

c# - 如何从 svg 扩展名 svg 中读取 xml 代码

转载 作者:太空宇宙 更新时间:2023-11-03 12:20:55 24 4
gpt4 key购买 nike

所以我在读取一些 xml 时遇到了问题。我需要阅读 <path>属性。

SVG 看起来像这样:

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg width="700pt" height="820pt" viewBox="60 25 500 600"
version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" >
<g id="id1">
<g id="id3">
<g id="id3">


<path clip-path="url(#SVG_CP_1)" fill="#000000" city="Amsterdam" />

</g>
</g>
</g>
</svg>

我的代码:

XDocument xdoc = XDocument.Load("D:/Users/me/Desktop/Website/files/test.svg");
xdoc.Descendants("path").Select(p => new
{
city= p.Attribute("city").Value,

}).ToList().ForEach(p =>
{
html.Append("City: " + p.city+ "<br>");


});

现在,当我排除 <svg width='700pt' etc... > 时,这段代码可以完美运行从 svg 文件。但我需要它在我的文件中。

最佳答案

根元素 svg 包含默认命名空间声明,其 URI 为 http://www.w3.org/2000/svg。请注意,所有没有前缀的后代元素都隐式继承祖先的默认 namespace 。

现在要在命名空间中引用元素,您可以结合使用 XNamespace 和目标元素的本地名称:

XDocument xdoc = ...;
XNamespace ns = "http://www.w3.org/2000/svg";
xdoc.Descendants(ns+"path").Select(p => new
{
....
})
....

关于c# - 如何从 svg 扩展名 svg 中读取 xml 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47325097/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com