【问题标题】:Adding xml:space to root element将 xml:space 添加到根元素
【发布时间】:2016-06-21 03:38:13
【问题描述】:

我有一个小问题,我认为这是不费吹灰之力的......但是唉......

我有一些 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是一个特殊属性,我有点怀疑。

所以:

它们是一样的吗?

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

【问题讨论】:

    标签: c# linq-to-xml xml-namespaces xattribute


    【解决方案1】:

    您只需要正确的 XName 值 - 我会使用这个:

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

    根据我的经验,XName +(XNamespace, string) 运算符通常是在 LINQ to XML 中使用命名空间的最简单方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多