c# - 如何在 SOAP header 中声明命名空间前缀

标签 c# soap header namespaces

我正在使用以下代码将 header 添加到soap header 。

using (new OperationContextScope(client.InnerChannel))
{
    OperationContext.Current.OutgoingMessageHeaders.Add(
        MessageHeader.CreateHeader("ns:To", "", "http://###.com"));                                
}

但是我得到了 undeclared namespace prefix ns错误,我需要添加 xmlns:ns="http://www.w3.org/2005/08/addressing"到命名空间。

所以我的问题是如何将命名空间前缀添加到
<s:Envelope>
    <s:Header>
        <ns:To>##</ns:To>
    </s:Header>
</s:Envelope>


<s:Envelope>
    <s:Header xmlns:ns="http://www.w3.org/2005/08/addressing">
        <ns:To>##</ns:To>
    </s:Header>
</s:Envelope>

最佳答案

试试这个:

String oasisWsSecNS = "http://www.w3.org/2005/08/addressing";
XmlDocument document = new XmlDocument();
     
XmlElement child = document.CreateElement("To", oasisWsSecNS);
child.AppendChild(document.CreateTextNode("[value]"));
element.AppendChild(child);
            
context.OutgoingMessageHeaders.Add(MessageHeader.CreateHeader("Header", oasisWsSecNS, document.DocumentElement));

关于c# - 如何在 SOAP header 中声明命名空间前缀,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51685756/

相关文章:

c# - 抽象类 > 子类的强制构造函数

c# - 搜索 IEnumerable<dynamic>

soap - Camel Caps SOAP 方法名称

java - jaxb 解码返回 null

xml - 元素或属性与 QName 生产不匹配

php - 增加标题的宽度

c# - DataSource 中的更新重置了 CheckedListBox 复选框

c# - 无法让模板缓冲区在 OpenTK 中工作,简单的 2D 四边形

c - 如何访问单独头文件中的定义值

c - 从一个 header 到另一个 header 中的结构的结构