/// <summary>
        /// Returns an XmlDocument over the message part content.
        /// </summary>
        /// <param name="messagePart">A pipeline messagePart part.</param>
        /// <returns>An XML document.</returns>
        public static XmlDocument AsXmlDocument(this IBaseMessagePart messagePart)
        {
            var bodyXml = new XmlDocument();

            // Preserve whitespace to render document as original by default
            bodyXml.PreserveWhitespace = true;

            if (messagePart.Data == null)
            {
                return(bodyXml);
            }

            var currentPosition = messagePart.Data.Position;

            messagePart.WithStreamAtStart();

            try
            {
                bodyXml.Load(messagePart.Data);
            }
            catch (XmlException)
            {
                return(new XmlDocument());
            }

            messagePart.Data.Seek(currentPosition, SeekOrigin.Begin);
            return(bodyXml);
        }
Example #2
0
        /// <summary>
        /// Populates a body part with content from an XML document.  If no body part exists, a new one
        /// is created.
        /// </summary>
        /// <param name="message">The massage to which the body part will be added.</param>
        /// <param name="part">The existing part</param>
        /// <param name="pc">The pipeline context.</param>
        /// <returns>A message with a body part populated by the XML document content.</returns>
        public static bool PopulateBodyPartFromExistingPart(this IBaseMessage message, IBaseMessagePart part, IPipelineContext pc)
        {
            if (message == null || part == null)
            {
                return(false);
            }

            Func <IBaseMessagePart> clonedPart = () =>
            {
                var newPart = pc.GetMessageFactory().CreateMessagePart();
                newPart.Data = part.GetOriginalDataStream().Clone();
                return(newPart);
            };

            var outPart = part.IsMutable
                ? clonedPart()
                : part.WithStreamAtStart();

            // Add a body part if none exists.
            if (message.BodyPart == null)
            {
                message.AddPart("Body", outPart, true);
            }

            return(true);
        }