Using xpath expressions in BizTalk

Xpath is a very nice way to retrieve values from BizTalk messages, especially when you can not use distinguished fields, for example in looping records. It can however be quite a complicated task as well, to find out how to retrieve a certain value. To that end, I have created a list of xpath filter expressions I commonly use. In these examples I will be using the following XML.

<root>
	<delivery>
		<deliverytype>Home</deliverytype>
		<boxinfo>
			<boxid>22</boxid>
		</boxinfo>
	</delivery>
	<delivery>
		<deliverytype>Work</deliverytype>
		<boxinfo>
			<boxid>35</boxid>
		</boxinfo>
	</delivery>
	<delivery>
		<deliverytype>Home</deliverytype>
		<boxinfo>
			<boxid>12</boxid>
			<boxid>87</boxid>
		</boxinfo>
	</delivery>
	<boxes>
		<box material="cardboard">
			<id>12</id>
			<contents>Envelopes</contents>
		</box>
		<box material="cardboard">
			<id>22</id>
			<contents>Surface Pro 2</contents>
		</box>
		<box material="plastic">
			<id>35</id>
			<contents>Stickers</contents>
		</box>
		<box material="cardboard">
			<id>87</id>
			<contents>Stamps</contents>
		</box>
	</boxes>
</root>
  • Filter on index
    Get the third delivery node.

    /*[local-name()='Root']/*[local-name()='Delivery'][2]

    Get the deliverytype node of the second delivery.

    /*[local-name()='Root']/*[local-name()='Delivery'][2]/*[local-name()='DeliveryType']
  • Filter on subnode text
    Get all delivery nodes, which have a deliverytype of Home.

    /*[local-name()='Root']/*[local-name()='Delivery'][*[local-name()='DeliveryType'][text()='Home']]

    Get the deliverytype node of the delivery for BoxID 87.

    /*[local-name()='Root']/*[local-name()='Delivery'][*[local-name()='BoxInfo']/*[local-name()='BoxID'][text()='87']]/*[local-name()='DeliveryType']

    Get the deliverytype node, of the box which contains the stickers.

    /*[local-name()='Root']/*[local-name()='Delivery'][*[local-name()='BoxInfo']/*[local-name()='BoxID'][text()=/*[local-name()='Root']/*[local-name()='Boxes']/*[local-name()='Box'][*[local-name()='Contents'][text()='Stickers']]/*[local-name()='ID']]]/*[local-name()='DeliveryType']
  • Continue reading

Converting String to XLangMessage (Streaming)

Often when you want to create a message in an orchestration, the option of using an XMLDocument is chosen for this. However, this option can be a serious performance hit, as your entire message will be loaded into memory, and can become as large as 10 times it’s original size. To avoid these performance hits, I have created 2 small helper methods, which allow you to instead use BizTalk’s native messages instead of an XMLDocument, and create the message in a streaming way. Using these, you can simply create a message from a string (and the other way around).
Continue reading