从WCF消息中获取正文

时间:2011-05-22 22:22:22

标签: wcf

我在从wcf消息中检索正文时遇到了一些麻烦。我正在尝试实现WCF消息检查器来验证针对XSD架构的消息。

肥皂体如下:

<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>

问题是,当我试图获取身体时,它只会获得部分身体信息。获取只有header元素,忽略GetMatchRequest元素(可能是因为多个命名空间...)

我正在使用以下来获取邮件正文:

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

我也试过以下:

bodyDoc.Load( message.GetReaderAtBodyContents());

上面的代码导致错误 - 该文档已经有一个'DocumentElement'节点。

任何人都可以帮助从WCF消息中提取正文吗?

由于

1 个答案:

答案 0 :(得分:6)

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