简体   繁体   English

将xml:space添加到根元素

[英]Adding xml:space to root element

I have a little problem that I thought was a no-brainer ... but alas ... 我有一个小问题,我认为这是一个不用脑子......但是唉......

I have some xml and all I want to do is to add the xml:space="preserve" to the root element using c#. 我有一些xml,我想要做的就是使用c#将xml:space="preserve"到根元素。

I tried this: 我试过这个:

var rootElem = xDoc.Root; // XDocument
rootElem.SetAttributeValue("{xml}space", "preserve");

The result of this is: 结果是:

<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">

I think this is equivalent to 认为这相当于

<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">

But since xml:space is a special attribute, I am a bit in doubt. 但由于xml:space是一个特殊的属性,我有点怀疑。

So: 所以:

Are they identical? 它们是一样的吗?

Is there a way I can add this to the document in a "clean" way? 有没有办法可以以“干净”的方式将其添加到文档中?

You just need the right XName value - I'd use this: 你只需要正确的XName值 - 我会用它:

doc.Root.SetAttributeValue(XNamespace.Xml + "space", "preserve");

The XName +(XNamespace, string) operator is generally the simplest way to work with namespaces in LINQ to XML, in my experience. 根据我的经验XName +(XNamespace, string)运算符通常是在LINQ to XML中使用名称空间的最简单方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM