Exemple #1
0
        static void Main(string[] args)
        {
            // The XPathMessageQueryCollection inherits from MessageQueryCollection.
            XPathMessageQueryCollection queryCollection = MessageHelper.SetupQueryCollection();


            // Create a message and a copy of the message. You must create a buffered copy to access the message body.
            Message       mess = MessageHelper.CreateMessage();
            MessageBuffer mb   = mess.CreateBufferedCopy(int.MaxValue);


            // Evaluate every query in the collection.
            foreach (XPathMessageQuery q in queryCollection)
            {
                // Evaluate the query. Note the result type is an XPathResult.
                XPathResult qPathResult = q.Evaluate <XPathResult>(mb);

                // Use the XPathResult to determine the result type.
                Console.WriteLine("Result type: {0}", qPathResult.ResultType);

                // The following code prints the result according to the result type.

                if (qPathResult.ResultType == XPathResultType.String)
                {
                    Console.WriteLine("{0} = {1}", q.Expression, qPathResult.GetResultAsString());
                }

                if (qPathResult.ResultType == XPathResultType.NodeSet)
                {
                    // Iterate through the node set.
                    XPathNodeIterator ns = qPathResult.GetResultAsNodeset();
                    foreach (XPathNavigator n in ns)
                    {
                        Console.WriteLine("\t{0} = {1}", q.Expression, n.Value);
                    }
                }
                if (qPathResult.ResultType == XPathResultType.Number)
                {
                    Console.WriteLine("\t{0} = {1}", q.Expression, qPathResult.GetResultAsNumber());
                }

                if (qPathResult.ResultType == XPathResultType.Boolean)
                {
                    Console.WriteLine("\t{0} ={1}", q.Expression, qPathResult.GetResultAsBoolean());
                }

                if (qPathResult.ResultType == XPathResultType.Error)
                {
                    Console.WriteLine("\tError!");
                }
            }

            Console.WriteLine();

            // The alternate code below demonstrates similar funcionality using a MessageQueryTable.
            // The difference is the KeyValuePair that requires a key to index each value.
            // The code uses the expression as the key, and an arbitrary value for the value.

            //MessageQueryTable<string> mq = MessageHelper.SetupTable();
            //foreach (KeyValuePair<MessageQuery, string> kv in mq)
            //{
            //    XPathMessageQuery xp = (XPathMessageQuery)kv.Key;
            //    Console.WriteLine("Value = {0}", kv.Value);
            //    Console.WriteLine("{0} = {1}", xp.Expression, xp.Evaluate<string>(mb));
            //}

            Console.ReadLine();
        }