【发布时间】:2011-07-20 20:03:48
【问题描述】:
我在XDocument 对象(LINQ to XML)中有一个 xml。我需要将命名空间添加到 Xdocument 中的每个 Xelement/node。
我不想以下面的方式添加。因为我已经在 xdoc 中有 xml。
XDocument xDoc = new XDocument(
new XElement(ns + "root",
new XElement(ns + "person",
new XAttribute("id", 1),
new XElement(ns + "firstname", "jack"),
下面是我的格式
<root>
<person>1</person>
<firstname>jack</firstname>
</root>
我想把它转换成下面这个格式
<emp:root>
<emp:person>1</emp:person>
<emp:firstname>jack</emp:firstname>
</emp:root>
【问题讨论】:
-
ns的声明在哪里?另外,你的意思是你已经在xDoc中有XML? -
XNamespace ns = "w3.org/2005/Atom";我将 xml 字符串加载到 xDocument 对象中。 Xdocument xDoc = 新 Xdocument(); xDoc.Parse(xml.tostring())