C# 向根元素添加xml:space

C# 向根元素添加xml:space,c#,linq-to-xml,xml-namespaces,xattribute,C#,Linq To Xml,Xml Namespaces,Xattribute,我有一个小问题,我认为这是一个不需要动脑筋的。。。但是,唉 我有一些xml,我想做的就是使用c将xml:space=“preserve”添加到根元素中 我试过这个: var rootElem = xDoc.Root; // XDocument rootElem.SetAttributeValue("{xml}space", "preserve"); 其结果是: <ProjectDetails xmlns="http://site/ppm" xmlns:xsd="http://www.w3

我有一个小问题,我认为这是一个不需要动脑筋的。。。但是,唉

我有一些xml,我想做的就是使用c将
xml:space=“preserve”
添加到根元素中

我试过这个:

var rootElem = xDoc.Root; // XDocument
rootElem.SetAttributeValue("{xml}space", "preserve");
其结果是:

<ProjectDetails xmlns="http://site/ppm" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" p3:space="preserve" xmlns:p3="xml">

我认为这相当于

<ProjectDetails xmlns="http://site/ppm" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xml:space="preserve">

但是由于
xml:space
是一个特殊属性,我有点怀疑

因此:

它们相同吗


有没有一种方法可以以“干净”的方式将其添加到文档中?

您只需要正确的
XName
值-我会使用以下方法:

doc.Root.SetAttributeValue(XNamespace.Xml + "space", "preserve");
根据我的经验,
XName+(XNamespace,string)
操作符通常是处理linqtoxml中名称空间的最简单方法