I'm using XElement
to build an XML document in C# and I'm trying to set
<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. My problem is that AFAICT the "xml" namespace prefix is somehow implicit and doesn't actually have a namespace URI associated with it. So how can I generate attributes with the namespace prefix "xml"?
Standard namespaces are available as properties on the XNamespace
class. Use that.
var myElement = doc.Descendants("myElement").Single();
myElement.SetAttributeValue(XNamespace.Xml + "space", "preserve");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.