使用C#转换XML名称空间前缀?

时间:2010-03-28 18:20:05

标签: c# java xml axis wss4j

我遇到了一个令人恼火的问题,即让Java服务客户端与WCF服务成功通信。我克服了许多障碍,我相信这是我的最后一个障碍。问题归结为Java Axis + WSS4J似乎如何处理xml命名空间。 Java平台似乎对xml名称空间前缀的期望非常严格,因此,不了解WCF回复消息。

我的问题简而言之如下。我的WCF服务中有一个类似于以下内容的xml响应:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" xmlns:a="http://www.w3.org/2005/08/addressing" xmlns:u="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
   <s:Header>
      <a:Action s:mustUnderstand="1" u:Id="_3">http://tempuri.org/IProcessor/DoProcessingResponse</a:Action>
      <h:CorrelationID xmlns:h="http://tempuri.org/">1234</h:CorrelationID>
      <a:RelatesTo u:Id="_4">uuid:40f800a0-9613-4f4a-96c5-b9fd98085deb</a:RelatesTo>
      <o:Security s:mustUnderstand="1" xmlns:o="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
         <!-- WS-Security header stuff -->
      </o:Security>
   </s:Header>
   <s:Body u:Id="_1">
      <e:EncryptedData Id="_2" Type="http://www.w3.org/2001/04/xmlenc#Content" xmlns:e="http://www.w3.org/2001/04/xmlenc#">
         <e:EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#aes128-cbc"/>
         <e:CipherData>
            <e:CipherValue>NfA6XunmyLlT2ucA+5QneoawHm+imcaCltDAJC1mRZOSxoB6YGpDLY1FyVykPbPGDoFGUESLsmvvbD62sNnRrgE+AuKPo+1CD3DF4LfurRcEv9A50ba9V+ViqlrhydhK</e:CipherValue>
         </e:CipherData>
      </e:EncryptedData>
   </s:Body>
</s:Envelope>

对于大多数事情,此响应使用简单的单字符名称空间前缀,例如SOAP信封的's',WS-Addressing的'a','WS-Security'的'o'等.Java客户端,即WSS4J似乎期待以下内容:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsa="http://www.w3.org/2005/08/addressing" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
   <soap:Header>
      <wsa:Action soap:mustUnderstand="1" wsu:Id="_3">http://tempuri.org/IProcessor/DoProcessingResponse</wsa:Action>
      <h:CorrelationID xmlns:h="http://tempuri.org/">1234</h:CorrelationID>
      <wsa:RelatesTo wsu:Id="_4">uuid:40f800a0-9613-4f4a-96c5-b9fd98085deb</a:RelatesTo>
      <wsse:Security soap:mustUnderstand="1" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
         <!-- WS-Security header stuff -->
      </wsse:Security>
   </soap:Header>
   <soap:Body u:Id="_1">
      <xenc:EncryptedData Id="_2" Type="http://www.w3.org/2001/04/xmlenc#Content" xmlns:xenc="http://www.w3.org/2001/04/xmlenc#">
         <xenc:EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#aes128-cbc"/>
         <xenc:CipherData>
            <xenc:CipherValue>NfA6XunmyLlT2ucA+5QneoawHm+imcaCltDAJC1mRZOSxoB6YGpDLY1FyVykPbPGDoFGUESLsmvvbD62sNnRrgE+AuKPo+1CD3DF4LfurRcEv9A50ba9V+ViqlrhydhK</xenc:CipherValue>
         </xenc:CipherData>
      </xenc:EncryptedData>
   </soap:Body>
</soap:Envelope>

收到我的响应消息后,Java客户端和WSS4J似乎想要通过自己的内部xml别名查找元素,例如WS-Addressing的'wsa'和WS-Security Extensions的'wsse'。由于实际响应xml中不存在这些命名空间,因此抛出异常。

我想知道是否有任何简单的方法可以使用C#,.NET和System.Xml命名空间将xml文档从一组命名空间转换为另一组命名空间。我已经尝试了一下XmlNamespaceManager,但它似乎并不完全支持我需要的东西......或者至少,我一直无法找到任何真正有用的例子,并且我不完全确定它是如何工作的。我正在努力避免编写一些重要的进程来自己手动处理这个,因为我不想在Java Axis / WSS4J客户端调用时大幅影响我们服务的性能。

1 个答案:

答案 0 :(得分:0)

这个特殊问题似乎源于WSS4J的旧版和错误版。较新的版本似乎没有这个问题,它不再是一个问题。