简体   繁体   English

如何在Cml中用不同的前缀在XmlNode中定义新的XML名称空间?

[英]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:

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). 父节点已经声明了wsse-Namespace,因此该节点不包含“ xmlns:wsse ='...'”-属性(未知的名称空间将在自动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 . 在这种情况下, wsu:xmlns是名称为xmlns且前缀为wsu的属性。 The namespace belonging to the prefix set in the form xmlns:wsu . 属于前缀的名称空间以xmlns:wsu的形式设置。

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

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