【问题标题】:XML generated by XMLWriter in WCF Custom Encoder generating namespace conflictWCF 自定义编码器中 XMLWriter 生成的 XML 生成命名空间冲突
【发布时间】:2012-04-26 11:38:33
【问题描述】:

我在我的 WCF 应用程序中使用自定义编码器来写出 SOAP 消息 XML。当我使用 WCF 中内置的默认 XML textMessageEncoding 时,这很好,但是当我使用自定义编码器时,我遇到了命名空间的问题 - 下面的 xmlns:a 标记(在元素和元素中) ) 为两个不同的命名空间定义了两次,这会在解析时导致服务端出现问题

<s: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" xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Header>
    <Action a:mustUnderstand="1" u:Id="_3" xmlns="http://www.w3.org/2005/08/addressing" **xmlns:a="http://schemas.xmlsoap.org/soap/envelope/"**></Action>
    <MessageID u:Id="_4" xmlns="http://www.w3.org/2005/08/addressing">
      <!--Omitted-->
    </MessageID>
    <ActivityId CorrelationId="1" xmlns="http://schemas.microsoft.com/2004/09/ServiceModel/Diagnostics">
      <!--Omitted-->
    </ActivityId>

关于如何解决此问题的任何想法?我在自定义编码器中使用 C# XmlWriter 来编写 XML,这似乎是导致问题的原因。

另外,我如何让XmlWriter 为上面的&lt;Action&gt; 标记使用前缀,以便它是&lt;a:Action&gt;,而不是为每个声明使用xmlns -

<Action a:mustUnderstand="1" u:Id="_3" xmlns="http://www.w3.org/2005/08/addressing"

这是我的 XmlWriterSettings

XmlWriterSettings writerSettings = new XmlWriterSettings();
writerSettings.Encoding = Encoding.GetEncoding(factory.CharSet);
writerSettings.OmitXmlDeclaration = true;
writerSettings.NamespaceHandling = NamespaceHandling.OmitDuplicates;  

【问题讨论】:

    标签: c# xml wcf soap xml-namespaces


    【解决方案1】:

    通过使用XmlDictionaryWriter 而不是XmlWriter 解决了这个问题。幸运的是,我使用的是 Utf-8 编码并且可以选择这样做

    【讨论】:

      猜你喜欢
      • 2017-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-13
      • 2012-02-06
      • 1970-01-01
      相关资源
      最近更新 更多