简体   繁体   中英

How to define a new XML namespace in an XmlNode with a different prefix in C#?

I've got an XmlNode that I create like this:

XmlNode nodeSecurity = xmlDoc.CreateNode(XmlNodeType.Element, "wsse", "Security", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");

The result looks like this:

<wsse:Security />

The wsse-Namespace has already been declared by a parent node, so this node does not contain an "xmlns:wsse='...'"-attribute (unknown namespaces would have been declared in an automatic xmlns-attribute).

Now my problem: I need to declare a new namespace here, so the result looks like this:

<wsse:Security wsu:xmlns='....' />

I tried to add an attribute like this:

XmlNode attr = xmlDoc.CreateNode(XmlNodeType.Attribute, "wsu", "blabla");
 nodeSecurity.Attributes.SetNamedItem(attr);

And the result is:

<wsse:Security p4:wsu="" xmlns:p4="blabla" />

Instead of:

<wsse:Security wsu:xmlns="blabla" />

What am I doing wrong here?

Try this

XmlAttribute attr = xmlDoc.CreateAttribute("wsu", "xmlns", "namespace");
attr.Value = "blabla";
nodeSecurity.Attributes.Append(attr);

Result

<wsse:Security wsu:xmlns="blabla" xmlns:wsu="namespace" />

In this case, wsu:xmlns is an attribute with the name xmlns and prefix wsu . The namespace belonging to the prefix set in the form xmlns:wsu .

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.

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