简体   繁体   English

如何在XElement中使用特殊的“xml”命名空间前缀

[英]How to use the special “xml” namespace prefix with XElement

I'm using XElement to build an XML document in C# and I'm trying to set 我正在使用XElement在C#中构建XML文档,我正在尝试设置

<myEelment xml:space="preserve">

Here's my current attempt: 这是我目前的尝试:

myElement.SetAttributeValue(XName.Get("space", "xml"), "preserve");

but it comes out like this: 但它出来是这样的:

<myEelment p4:space="preserve" xmlns:p4="xml">

I understand how this is going wrong - my code is using "xml" as a namespace URI when I want to use as a namespace prefix. 我理解这是怎么回事 - 当我想用作命名空间前缀时,我的代码使用“xml”作为命名空间URI。 My problem is that AFAICT the "xml" namespace prefix is somehow implicit and doesn't actually have a namespace URI associated with it. 我的问题是AFAICT“xml”名称空间前缀在某种程度上是隐式的,并且实际上没有与之关联的名称空间URI。 So how can I generate attributes with the namespace prefix "xml"? 那么如何使用命名空间前缀“xml”生成属性?

Standard namespaces are available as properties on the XNamespace class. 标准名称空间可用作XNamespace类的属性。 Use that. 用那个。

var myElement = doc.Descendants("myElement").Single();
myElement.SetAttributeValue(XNamespace.Xml + "space", "preserve");

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

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