从 WCF 消息中获取正文 [英] Get body from WCF message

查看:24
本文介绍了从 WCF 消息中获取正文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在从 wcf 消息中检索正文时遇到了一些麻烦.我正在尝试实现 WCF 消息检查器以根据 XSD 架构验证消息.

I'm having a bit of trouble retrieving body from a wcf message. I am trying to implement WCF message inspector to validate messages against XSD schema.

皂体如下所示:

<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <Header xmlns="http://www.test1.com">
      <applicationID>1234</applicationID>
    </Header>
    <GetMatchRequest xmlns="http://www.tempuri.org">test</GetMatchRequest>
  </s:Body>

问题是当我尝试获取正文时,它只能获取部分正文消息.只获取头元素,忽略 GetMatchRequest 元素(可能是因为多个命名空间...)

The problem is when I try to get body it only gets partial body message. Gets only header element, ignores GetMatchRequest element(may be because of multiple namespaces…)

我正在使用以下内容来获取消息正文:

I am using following to get message body:

XmlDocument bodyDoc = new XmlDocument();
bodyDoc.Load( message.GetReaderAtBodyContents().ReadSubtree());

我也尝试了以下操作:

bodyDoc.Load( message.GetReaderAtBodyContents());

上面的代码导致错误 - 此文档已经有一个DocumentElement"节点.

The code above results in error - This document already has a 'DocumentElement' node.

谁能帮忙从 WCF 消息中提取正文?

Can anyone please help in extracting body from a WCF message?

谢谢

推荐答案

Message.GetReaderAtBodyContents 返回的读取器不是位于元素,而是位于其第一个子元素.通常消息正文只包含一个根元素,因此您可以直接加载它.但是在您的消息中它包含多个根元素(Header 和 GetMatchRequest),因此如果您想在 XmlDocument 中加载整个正文,则需要提供一个包装元素(XmlDocument 只能有一个根元素).在下面的示例中,我使用 作为包装元素,但您可以使用任何您想要的元素.代码只是读取正文,直到找到结束元素 (</s:Body>).

Message.GetReaderAtBodyContents return the reader positioned not at the element, but at its first child. Usually the message body contains only a single root element, so you can load it directly. But in your message it contains multiple root elements (Header and GetMatchRequest), so if you want to load the whole body in a XmlDocument, you need to provide a wrapping element (the XmlDocument can have only one root element). In the example below I use <s:Body> as the wrapping element, but you could use anything you want. The code simply reads the body until it finds the end element (</s:Body>).

    public class Post_a866abd2_bdc2_4d30_8bbc_2ce46df38dc4
    {
        public static void Test()
        {
            string xml = @"<s:Envelope xmlns:s=""http://schemas.xmlsoap.org/soap/envelope/"">
  <s:Body xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">
    <Header xmlns=""http://www.test1.com"">
      <applicationID>1234</applicationID>
    </Header>
    <GetMatchRequest xmlns=""http://www.tempuri.org"">test</GetMatchRequest>
  </s:Body>
</s:Envelope>";
            Message message = Message.CreateMessage(XmlReader.Create(new StringReader(xml)), int.MaxValue, MessageVersion.Soap11);
            Console.WriteLine(message);
            XmlDocument bodyDoc = new XmlDocument();
            MemoryStream ms = new MemoryStream();
            XmlWriter w = XmlWriter.Create(ms, new XmlWriterSettings { Indent = true, IndentChars = "  ", OmitXmlDeclaration = true });
            XmlDictionaryReader bodyReader = message.GetReaderAtBodyContents();
            w.WriteStartElement("s", "Body", "http://schemas.xmlsoap.org/soap/envelope/");
            while (bodyReader.NodeType != XmlNodeType.EndElement && bodyReader.LocalName != "Body" && bodyReader.NamespaceURI != "http://schemas.xmlsoap.org/soap/envelope/")
            {
                if (bodyReader.NodeType != XmlNodeType.Whitespace)
                {
                    w.WriteNode(bodyReader, true);
                }
                else
                {
                    bodyReader.Read(); // ignore whitespace; maintain if you want
                }
            }
            w.WriteEndElement();
            w.Flush();
            Console.WriteLine(Encoding.UTF8.GetString(ms.ToArray()));
            ms.Position = 0;
            XmlDocument doc = new XmlDocument();
            doc.Load(ms);
            Console.WriteLine(doc.DocumentElement.OuterXml);
        }
    }

这篇关于从 WCF 消息中获取正文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆