/// <summary> /// Remove outer nodes from the message part content and leave the body. /// </summary> /// <param name="messagePart">The message part</param> /// <param name="bodyContainerXPath">The XPath specifying the body of the message.</param> /// <returns>The message part with the envelope removed.</returns> public static IBaseMessagePart RemoveEnvelope(this IBaseMessagePart messagePart, string bodyContainerXPath) { if (string.IsNullOrEmpty(bodyContainerXPath)) { return(messagePart); } var bodyXml = messagePart.AsXmlDocument(); // Assign content back to part var contentStream = new MemoryStream(); var sw = new StreamWriter(contentStream); var selectSingleNode = bodyXml.SelectSingleNode(bodyContainerXPath); if (selectSingleNode != null) { sw.Write(selectSingleNode.FirstChild.OuterXml); } sw.Flush(); contentStream.StreamAtStart(); messagePart.Data = contentStream; return(messagePart); }