gpt4 book ai didi

c# - 添加 xml :space to root element

转载 作者:太空狗 更新时间:2023-10-30 00:30:29 25 4
gpt4 key购买 nike

我有一个我认为很简单的小问题......但是唉......

我有一些 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) 运算符通常是在 LINQ to XML 中使用 namespace 的最简单方法。

关于c# - 添加 xml :space to root element,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35846252/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com