Get body from WCF message

I am having trouble retrieving the body from a wcf message. I am trying to implement a WCF message inspector to check messages using the XSD scheme.

The body of the soap is as follows:

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

The problem is that when I try to get the body, it only receives a partial message from the body. Gets only the title element, ignores the GetMatchRequest element (maybe due to multiple namespaces ...)

I use the following to get the message body:

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

I also tried the following:

 bodyDoc.Load( message.GetReaderAtBodyContents()); 

The above code results in an error. This document already has a "DocumentElement" node.

Can anyone help in extracting the body from the WCF message?

thanks

+1
source share
1 answer

Message.GetReaderAtBodyContents returns a reader located not on the element, but on its first child element. Typically, a message body contains only one root element, so you can download it directly. But in your message, it contains several root elements (Header and GetMatchRequest), so if you want to load the whole object in an XmlDocument, you need to provide a wrapper element (XmlDocument can have only one root element). In the example below, I use <s:Body> as a wrapping element, but you can use whatever you want. The code simply reads the body until it finds the final 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); } } 
+6
source

All Articles