예제 #1
0
        public async Task <Message[]> TransformAsync(params Message[] inputMessages)
        {
            var inputXpathDoc = await InputMessageHelper.CreateXPathDocumentAsync(inputMessages);

            var outputMessages = new List <Message>();

            // Execute the transform:
            using (var outputStream = new MemoryStream())
            {
                _xslTransform.Transform(inputXpathDoc, null, outputStream);

                // Rewind the stream
                outputStream.Position = 0;

                using (var outputReader = XmlReader.Create(outputStream))
                {
                    // Load the message:
                    var outputMessageNavigator = new XPathDocument(outputReader).CreateNavigator();

                    outputMessageNavigator.MoveToFirstChild();

                    // If this is a multi-part output message, create a new message for each part:
                    if (string.Equals(outputMessageNavigator.LocalName, Constants.MultipartRootElement) &&
                        string.Equals(outputMessageNavigator.NamespaceURI, Constants.MultipartNamespace))
                    {
                        var messageParts = outputMessageNavigator.Select(string.Format("/*[local-name()='{0}' and namespace-uri()='{2}']/*[starts-with(local-name(), '{1}') and namespace-uri()='{2}']",
                                                                                       Constants.MultipartRootElement,
                                                                                       Constants.MultipartPartElement,
                                                                                       Constants.MultipartNamespace));

                        foreach (XPathNavigator part in messageParts)
                        {
                            var msg = new Message {
                                ContentStream = new MemoryStream(Encoding.UTF8.GetBytes(part.InnerXml))
                            };
                            outputMessages.Add(msg);
                        }
                    }
                    else
                    {
                        // Rewind the stream
                        outputStream.Position = 0;

                        // Copy the result to an output message:
                        var msg = new Message {
                            ContentStream = new MemoryStream()
                        };
                        await outputStream.CopyToAsync(msg.ContentStream);

                        // Rewind the stream
                        msg.ContentStream.Position = 0;

                        outputMessages.Add(msg);
                    }
                }
            }

            return(outputMessages.ToArray());
        }
예제 #2
0
        public static async Task <T> EvaluateAsync <T>(string xPathExpression, params Message[] msgs)
        {
            var inputXpathDoc = await InputMessageHelper.CreateXPathDocumentAsync(msgs);

            var navigator = inputXpathDoc.CreateNavigator();

            return((T)navigator.Evaluate(xPathExpression));
        }