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

兔子-顾问 2014-11-21 01:51:06
我需要生成这样的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”。

请教如何处理呢?一定要自己拼字符串么?
...全文
788 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
q107770540 2014-11-21
  • 打赏
  • 举报
回复
	 XDocument document = new XDocument(new XDeclaration("1.0", "UTF-8", "no"));
	 XNamespace ns = "http://www.w3.org/2000/svg";  
	XElement root = new XElement(ns+"svg");
	root.Add(
		new XAttribute("width", 1)
		, new XAttribute("height", 2)
		, new XAttribute("viewBox", "0 0 " + 3)
		, new XAttribute("preserveAspectRatio", "xMidYMid"),
		 new XAttribute("xmlns", "http://www.w3.org/2000/svg"),
			new XElement( ns+"Child1", "child content"),
			new XElement( ns+"Child2", "child content")
			);
	    //Add element
		root.AddFirst(new XElement( ns+"Child0", "child content"));	
			
		document.Add(root);
q107770540 2014-11-21
  • 打赏
  • 举报
回复
	 XDocument document = new XDocument(new XDeclaration("1.0", "UTF-8", "no"));
	 XNamespace ns = "http://www.w3.org/2000/svg";  
	XElement root = new XElement(ns+"svg");
	root.Add(
		new XAttribute("width", 1)
		, new XAttribute("height", 2)
		, new XAttribute("viewBox", "0 0 " + 3)
		, new XAttribute("preserveAspectRatio", "xMidYMid"),
		 new XAttribute("xmlns", "http://www.w3.org/2000/svg"),
			new XElement( ns+"Child1", "child content"),
			new XElement( ns+"Child2", "child content")
			);
	    //Add element
		root.AddFirst(new XElement( ns+"Child0", "child content"));	
			
		document.Add(root);
q107770540 2014-11-21
  • 打赏
  • 举报
回复
引用 4 楼 wuyazhe 的回复:
#1,#2 插入的的确没问题了,不过子节点都会多一个属性:xmlns="" 这个固定多余的,能去掉么?
可以去掉的:
void Main()
{
	 XDocument document = new XDocument(new XDeclaration("1.0", "UTF-8", "no"));
	 XNamespace ns = "http://www.w3.org/2000/svg";  
	XElement root = new XElement(ns+"svg");
	root.Add(
		new XAttribute("width", 1)
		, new XAttribute("height", 2)
		, new XAttribute("viewBox", "0 0 " + 3)
		, new XAttribute("preserveAspectRatio", "xMidYMid"),
		 new XAttribute("xmlns", "http://www.w3.org/2000/svg"),
			new XElement( ns+"Child1", "child content"),
			new XElement( ns+"Child2", "child content")
			);
		document.Add(root);
		document.Save("d:\\test1.xml");
}
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<svg width="1" height="2" viewBox="0 0 3" preserveAspectRatio="xMidYMid" xmlns="http://www.w3.org/2000/svg">
  <Child1>child content</Child1>
  <Child2>child content</Child2>
</svg>
兔子-顾问 2014-11-21
  • 打赏
  • 举报
回复
#1,#2 插入的的确没问题了,不过子节点都会多一个属性:xmlns="" 这个固定多余的,能去掉么?
hb1122 2014-11-21
  • 打赏
  • 举报
回复

            System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
            System.Xml.XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", "UTF-8", "no");
            doc.AppendChild(declaration);
            System.Xml.XmlElement root = doc.CreateElement("svg", "http://www.w3.org/2000/svg");

            System.Xml.XmlElement line = doc.CreateElement("line", "http://www.w3.org/2000/svg");

            line.SetAttribute("x1","0");
            line.SetAttribute("y1","0");
            line.SetAttribute("x2","200");
            line.SetAttribute("y2","200");
            line.SetAttribute("style","stroke:rgb(255,0,0);stroke-width:2");
            doc.AppendChild(root);
            root.AppendChild(line);
q107770540 2014-11-21
  • 打赏
  • 举报
回复
用 XNamespace ns = "http://www.w3.org/2000/svg"; http://blog.csdn.net/q107770540/article/details/6117004
void Main()
{
	XDocument document = new XDocument(new XDeclaration("1.0", "UTF-8", "no"));
	 XNamespace ns = "http://www.w3.org/2000/svg";  
	XElement root = new XElement(ns+"svg");
	root.Add(
		new XAttribute("width", 1)
		, new XAttribute("height", 2)
		, new XAttribute("viewBox", "0 0 " + 3)
		
		, new XAttribute("preserveAspectRatio", "xMidYMid"));
		document.Add(root);
		document.Save("d:\\test.xml");
}

110,535

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧