Export (0) Print
Expand All

XPathMessageQueryCollection Class

Contains a collection of XPathMessageQuery objects.

Namespace:  System.ServiceModel.Dispatcher
Assembly:  System.ServiceModel (in System.ServiceModel.dll)

'Declaration
Public Class XPathMessageQueryCollection _
	Inherits MessageQueryCollection

The XPathMessageQueryCollection type exposes the following members.

  NameDescription
Public methodXPathMessageQueryCollectionInitializes a new instance of the XPathMessageQueryCollection class.
Top

  NameDescription
Public propertyCountGets the number of elements actually contained in the Collection(Of T). (Inherited from Collection(Of T).)
Public propertyItemGets or sets the element at the specified index. (Inherited from Collection(Of T).)
Protected propertyItemsGets a IList(Of T) wrapper around the Collection(Of T). (Inherited from Collection(Of T).)
Top

  NameDescription
Public methodAddAdds an object to the end of the Collection(Of T). (Inherited from Collection(Of T).)
Public methodClearRemoves all elements from the Collection(Of T). (Inherited from Collection(Of T).)
Protected methodClearItemsRemoves all elements from the Collection(Of T). (Inherited from Collection(Of T).)
Public methodContainsDetermines whether an element is in the Collection(Of T). (Inherited from Collection(Of T).)
Public methodCopyToCopies the entire Collection(Of T) to a compatible one-dimensional Array, starting at the specified index of the target array. (Inherited from Collection(Of T).)
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Public methodEvaluate(Of TResult)(Message)Runs a query against the message. The body cannot be queried. (Overrides MessageQueryCollection.Evaluate(Of TResult)(Message).)
Public methodEvaluate(Of TResult)(MessageBuffer)Runs an XPath query against the message. (Overrides MessageQueryCollection.Evaluate(Of TResult)(MessageBuffer).)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetEnumeratorReturns an enumerator that iterates through the Collection(Of T). (Inherited from Collection(Of T).)
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodIndexOfSearches for the specified object and returns the zero-based index of the first occurrence within the entire Collection(Of T). (Inherited from Collection(Of T).)
Public methodInsertInserts an element into the Collection(Of T) at the specified index. (Inherited from Collection(Of T).)
Protected methodInsertItemInserts the XPath query object into the collection at the specified index. (Overrides Collection(Of T).InsertItem(Int32, T).)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodRemoveRemoves the first occurrence of a specific object from the Collection(Of T). (Inherited from Collection(Of T).)
Public methodRemoveAtRemoves the element at the specified index of the Collection(Of T). (Inherited from Collection(Of T).)
Protected methodRemoveItemRemoves the item from the collection at the specified index. (Overrides Collection(Of T).RemoveItem(Int32).)
Protected methodSetItemReplaces the element at the specified index. (Overrides Collection(Of T).SetItem(Int32, T).)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

  NameDescription
Explicit interface implemetationPrivate methodICollection.CopyToCopies the elements of the ICollection to an Array, starting at a particular Array index. (Inherited from Collection(Of T).)
Explicit interface implemetationPrivate propertyICollection(Of T).IsReadOnlyGets a value indicating whether the ICollection(Of T) is read-only. (Inherited from Collection(Of T).)
Explicit interface implemetationPrivate propertyICollection.IsSynchronizedGets a value indicating whether access to the ICollection is synchronized (thread safe). (Inherited from Collection(Of T).)
Explicit interface implemetationPrivate propertyICollection.SyncRootGets an object that can be used to synchronize access to the ICollection. (Inherited from Collection(Of T).)
Explicit interface implemetationPrivate methodIEnumerable.GetEnumeratorReturns an enumerator that iterates through a collection. (Inherited from Collection(Of T).)
Explicit interface implemetationPrivate methodIList.AddAdds an item to the IList. (Inherited from Collection(Of T).)
Explicit interface implemetationPrivate methodIList.ContainsDetermines whether the IList contains a specific value. (Inherited from Collection(Of T).)
Explicit interface implemetationPrivate methodIList.IndexOfDetermines the index of a specific item in the IList. (Inherited from Collection(Of T).)
Explicit interface implemetationPrivate methodIList.InsertInserts an item into the IList at the specified index. (Inherited from Collection(Of T).)
Explicit interface implemetationPrivate propertyIList.IsFixedSizeGets a value indicating whether the IList has a fixed size. (Inherited from Collection(Of T).)
Explicit interface implemetationPrivate propertyIList.IsReadOnlyGets a value indicating whether the IList is read-only. (Inherited from Collection(Of T).)
Explicit interface implemetationPrivate propertyIList.ItemGets or sets the element at the specified index. (Inherited from Collection(Of T).)
Explicit interface implemetationPrivate methodIList.RemoveRemoves the first occurrence of a specific object from the IList. (Inherited from Collection(Of T).)
Top

For more information about message queries, see the MessageQuery and MessageQueryCollection classes.

The following example creates a message and XPath message queries. The queries are evaluated by the XPathMessageQuery objects contained in an XPathMessageQueryCollection object. The results of each query are tested using the ResultType property of the XPathResult class.

Imports System.IO
Imports System.Xml
Imports System.ServiceModel.Dispatcher
Imports System.ServiceModel
Imports System.ServiceModel.Channels
Imports System.Xml.XPath

Namespace MessageQueryExamples


    Public Class Program

        Public Shared Sub Main(ByVal args As String())

            ' The XPathMessageQueryCollection inherits from MessageQueryCollection. 
            Dim queryCollection As XPathMessageQueryCollection = MessageHelper.SetupQueryCollection()


            ' Create a message and a copy of the message. You must create a buffered copy to access the message body. 
            Dim mess As Message = MessageHelper.CreateMessage()
            Dim mb As MessageBuffer = mess.CreateBufferedCopy(Integer.MaxValue)


            ' Evaluate every query in the collection.  
            Dim q As XPathMessageQuery
            For Each q In queryCollection

                ' Evaluate the query. Note the result type is an XPathResult. 
                Dim qPathResult As XPathResult = q.Evaluate(Of 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 Then
                    Console.WriteLine("{0} = {1}", q.Expression, qPathResult.GetResultAsString())
                End If 
                If (qPathResult.ResultType = XPathResultType.NodeSet) Then 

                    ' Iterate through the node set. 
                    Dim ns As XPathNodeIterator = qPathResult.GetResultAsNodeset()
                    Dim n As XPathNavigator
                    For Each n In ns
                        Console.WriteLine("     {0} = {1}", q.Expression, n.Value)
                    Next 
                End If 
                If qPathResult.ResultType = XPathResultType.Number Then
                    Console.WriteLine("    {0} = {1}", q.Expression, qPathResult.GetResultAsNumber())
                End If 
                If qPathResult.ResultType = XPathResultType.Boolean Then
                    Console.WriteLine("    {0} ={1}", q.Expression, qPathResult.GetResultAsBoolean())
                End If 

                If qPathResult.ResultType = XPathResultType.Error Then
                    Console.WriteLine("    Error!")
                End If 

            Next

            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.            

            'Dim mq As MessageQueryTable(Of String) = MessageHelper.SetupTable() 
            'Dim kv As KeyValuePair(Of MessageQuery, String) 
            'For Each kv In mq 
            '    ' 
            '    Dim xp As XPathMessageQuery = CType(kv.Key, XPathMessageQuery) 
            '    Console.WriteLine("Value = {0}", kv.Value) 
            '    Console.WriteLine("{0} = {1}", xp.Expression, xp.Evaluate(Of String)(mb)) 
            'Next

            Console.ReadLine()
        End Sub 
        Private Shared Sub Evaluate(ByVal p1 As Object)
            Throw New NotImplementedException
        End Sub 
    End Class 

    Public Class MessageHelper

        Shared messageBody As String = _
              "<PurchaseOrder date='today'>" + _
                  "<Number>ABC-2009-XYZ</Number>" + _
                  "<Department>OnlineSales</Department>" + _
                  "<Items>" + _
                      "<Item product='nail' quantity='1'>item1</Item>" + _
                      "<Item product='screw' quantity='2'>item2</Item>" + _
                      "<Item product='brad' quantity='3'>" + _
                          "<SpecialOffer/>" + _
                          "Special item4" + _
                      "</Item>" + _
                      "<Item product='SpecialNails' quantity='9'>item5</Item>" + _
                      "<Item product='SpecialBrads' quantity='11'>" + _
                          "<SpecialOffer/>" + _
                          "Special item6" + _
                      "</Item>" + _
                      "<Item product='hammer' quantity='1'>item7</Item>" + _
                      "<Item product='wrench' quantity='2'>item8</Item>" + _
                  "</Items>" + _
                "<Comments>" + _
                "Rush order" + _
                "</Comments>" + _
              "</PurchaseOrder>" 

        Public Shared xpath As String = "/s12:Envelope/s12:Body/PurchaseOrder/Items/Item[@quantity = 1]" 
        Public Shared xpath2 As String = "/s12:Envelope/s12:Body/PurchaseOrder/Items/Item[@product = 'nail']" 
        Public Shared xpath3 As String = "/s12:Envelope/s12:Body/PurchaseOrder/Comments" 
        Public Shared xpath4 As String = "count(/s12:Envelope/s12:Body/PurchaseOrder/Items/Item)" 
        Public Shared xpath5 As String = "substring(string(/s12:Envelope/s12:Body/PurchaseOrder/Number),5,4)" 
        Public Shared xpath6 As String = "/s12:Envelope/s12:Body/PurchaseOrder/Department='OnlineSales'" 
        Public Shared xpath7 As String = "//PurchaseOrder/@date" 
        Public Shared xpath8 As String = "//SpecialOffer/ancestor::Item[@product = 'brad']" 

        ' Invoke the correlation data function. 


        Public Shared xpath9 As String = "sm:correlation-data('CorrelationData1')" 
        Public Shared xpath10 As String = "sm:correlation-data('CorrelationData2')" 

        Public Shared xpath11 As String = "/s12:Envelope/s12:Body/PurchaseOrder/Items/Item[@quantity = 2]" 


        Public Shared Function CreateMessage() As Message

            Dim stringReader As New StringReader(messageBody)
            Dim xmlReader As New XmlTextReader(stringReader)
            Dim message As Message = message.CreateMessage( _
                MessageVersion.Soap12WSAddressing10, "http://purchaseorder", xmlReader)

            ' Add two correlation properties using lambda expressions. The property names are 
            ' CorrelationData1 and CorrelationData2. The first goes to "value1" and the 
            ' second to "value2". You can use your own property names and values. 
            Dim data As New CorrelationDataMessageProperty()

            data.Add("CorrelationData1", Function() "value1")
            data.Add("CorrelationData2", Function() "value2")
            message.Properties(CorrelationDataMessageProperty.Name) = data


            Return message
        End Function 


        Public Shared Function SetupQueryCollection() As XPathMessageQueryCollection

            ' Create the query collection and add the XPath queries to it. To create 
            ' the query, you must also use a new XPathMessageContext. 

            Dim queryCollection As New XPathMessageQueryCollection()

            Dim context As XPathMessageContext = New XPathMessageContext()
            queryCollection.Add(New XPathMessageQuery(xpath, context))
            queryCollection.Add(New XPathMessageQuery(xpath2, context))
            queryCollection.Add(New XPathMessageQuery(xpath3, context))
            queryCollection.Add(New XPathMessageQuery(xpath4, context))
            queryCollection.Add(New XPathMessageQuery(xpath5, context))
            queryCollection.Add(New XPathMessageQuery(xpath6, context))
            queryCollection.Add(New XPathMessageQuery(xpath7, context))
            queryCollection.Add(New XPathMessageQuery(xpath8, context))
            queryCollection.Add(New XPathMessageQuery(xpath9, context))
            queryCollection.Add(New XPathMessageQuery(xpath10, context))
            queryCollection.Add(New XPathMessageQuery(xpath11, context))

            Return queryCollection
        End Function 

        Public Shared Function SetupTable() As MessageQueryTable(Of String)

            ' This is optional code to demonstrate using a MessageQueryTable. 
            ' Compare this to the MessageQueryCollection. 
            Dim table As MessageQueryTable(Of String) = New MessageQueryTable(Of String)()
            Dim context As XPathMessageContext = New XPathMessageContext()


            ' The code adds a KeyValuePair to the table. Each pair requires 
            ' a query used as the Key, and a value that is paired to the key.
            table.Add(New XPathMessageQuery(xpath, context), "value10")
            table.Add(New XPathMessageQuery(xpath2, context), "value20")
            table.Add(New XPathMessageQuery(xpath3, context), "value30")
            table.Add(New XPathMessageQuery(xpath4, context), "value40")
            table.Add(New XPathMessageQuery(xpath5, context), "value50")
            table.Add(New XPathMessageQuery(xpath6, context), "value60")
            table.Add(New XPathMessageQuery(xpath7, context), "value70")
            table.Add(New XPathMessageQuery(xpath8, context), "value80")
            table.Add(New XPathMessageQuery(xpath9, context), "value90")
            table.Add(New XPathMessageQuery(xpath10, context), "value100")
            table.Add(New XPathMessageQuery(xpath11, context), "value110")
            Return table
        End Function 
    End Class 
End Namespace

.NET Framework

Supported in: 4.5, 4

.NET Framework Client Profile

Supported in: 4

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Show:
© 2014 Microsoft