【发布时间】:2022-07-22 16:19:02
【问题描述】:
我有一个 svg 文件,我想使用 c# 和 XPath 在其中插入一个圆圈:
这是之前的文件内容:
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="594.75pt" height="841.5pt" viewBox="0 0 594.75 841.5" style="border: 1px solid red">
<circle cx="50%" cy="50%" r="50" fill="red" />
</svg>
我想要这个文件内容之后:
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="594.75pt" height="841.5pt" viewBox="0 0 594.75 841.5" style="border: 1px solid red">
<circle cx="50%" cy="50%" r="50" fill="red" />
<circle stroke="blue" stroke-width="3" cx="148.63pt" cy="401.02pt" r="84.15" fill-opacity="0.1"/>
</svg>
我正在使用以下代码:
XmlDocument doc = new XmlDocument();
doc.Load(@"D:\\test.svg");
XmlNamespaceManager xmlnsManager = new XmlNamespaceManager(doc.NameTable);
xmlnsManager.AddNamespace("xlink", "http://www.w3.org/1999/xlink");
xmlnsManager.AddNamespace("svg", "http://www.w3.org/2000/svg");
XmlDocumentFragment xmlDocFrag = doc.CreateDocumentFragment();
xmlDocFrag.InnerXml = @"<circle stroke=\"blue\" stroke-width=\"3\" cx=\"148.63pt\" cy=\"401.02pt\" r=\"84.15\" fill-opacity=\"0.1\"/>";
XmlElement mapElement = (XmlElement)doc.SelectSingleNode(@"//svg[last()]", xmlnsManager);
mapElement.AppendChild(xmlDocFrag); // <----- null !!
doc.Save(path);
但是,在 SelectSingleNode 之后 mapElement 为 null :( 我希望它是现有的圆形元素。
【问题讨论】: