Jes*_*olm 4 c# linq-to-xml xml-namespaces xattribute
我有一个小问题,我认为这是一个不用脑子......但是唉......
我有一些xml,我想要做的就是xml:space="preserve"使用c#添加到根元素.
我试过这个:
var rootElem = xDoc.Root; // XDocument
rootElem.SetAttributeValue("{xml}space", "preserve");
Run Code Online (Sandbox Code Playgroud)
结果是:
<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">
Run Code Online (Sandbox Code Playgroud)
我认为这相当于
<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">
Run Code Online (Sandbox Code Playgroud)
但由于这xml:space是一个特殊的属性,我有点怀疑.
所以:
它们是一样的吗?
有没有办法可以以"干净"的方式将其添加到文档中?
你只需要正确的XName价值 - 我会用它:
doc.Root.SetAttributeValue(XNamespace.Xml + "space", "preserve");
Run Code Online (Sandbox Code Playgroud)
根据XName +(XNamespace, string)我的经验,运算符通常是在LINQ to XML中使用命名空间的最简单方法.