C#中如何插入XML的命名空间

兔子-顾问 兔子-顾问 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
我需要生成这样的XML数据:

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<svg xmlns="http://www.w3.org/2000/svg" version="1.1">
<line x1="0" y1="0" x2="200" y2="200" style="stroke:rgb(255,0,0);stroke-width:2" />
</svg>

我使用如下代码

XDocument document = new XDocument(new XDeclaration("1.0", "UTF-8", "no"));
XElement root = new XElement("svg");
root.Add(
new XAttribute("width", this.Size.Width)
, new XAttribute("height", this.Size.Height)
, new XAttribute("viewBox", "0 0 " + this.Size.Width.ToString() + " " + this.Size.Height.ToString())
, new XAttribute("xmlns", "http://www.w3.org/2000/svg")
, new XAttribute("preserveAspectRatio", "xMidYMid"));
document.Add(root);


编译时正确,但运行时会有异常:
在同一开始元素标记中,无法将前缀“”从“”重定义为“http://www.w3.org/2000/svg”。

请教如何处理呢?一定要自己拼字符串么?
...全文

相关推荐

评论 7
收藏 已收藏
分享
返回顶部
兔子-顾问
C# 111,129 成员 642,538 内容