Saturday, May 30, 2009

BizTalk: Message Assignment shape. Assign value from one Schema to another Schema.

We can assign one schema to another by many ways. But best way to assign values to one schema from another schema is to use the Message assignment Shape inside the Message Construct shape. Let’s take an example for it.

Example: Conversion of BillingAddress to ShippingAddress. For this I have two Schemas BillingAddress.xsd and ShippingAddress.xsd. All the elements of both schemas are promoted to Distinguished Fields, so that we can access them in message assignment shape. Both schemas are as follows:

BillingAddress.xsd

ShippingAddress.xsd:

Now I have created an orchestration, in which I am assigning the values of BillingAddress.xsd to ShippingAddress.xsd. For this I have created two messages msgBillingAddress and msgShippingAddress of BillingAddress.xsd and ShippingAddress.xsd types respectively. I have created a variable named varXmlDoc of System.Xml.XmlDocument type. My orchestration will look like the follows:

You can see in the image that there I have added a Receive Shape which will accept the msgBillingAddress and at the last you can see that I have added a Send Shape to send the ShippingAddress using msgShippingAddress. Between these two shapes(Receive & Send Shape), I have added a shape through which I am assigning values of BillingAddress to ShippingAddress message. And for this I have added a Construct shape and inside it I have added a Message Assignment Shape.

Now the purpose of this article is to share how to assign one message to another one. I have added the following code to assign BillingAddress to ShippingAddress message.

//Create a new object of XMLDocument. For this we will use the Variable created as XMLDocument.

varXmlDoc = new System.Xml.XmlDocument();

//We need to Load the variable with dummy XML.

//It can be same as generated instance.

//Note here we have to assign that instance that we need in target, not the //source schema.

varXmlDoc.LoadXml(@"<ns0:ShippingAddress xmlns:ns0=""http://SampleXPath.ShippingAddress"">

<Address1>Test Address1</Address1>

<Address2> Test Address2</Address2>

<City>Test City</City>

<State>Test State</State>

<Country>Test Country</Country>

<Zip>00000</Zip>

</ns0:ShippingAddress>");

//Assign XMLDocument varSmlDoc variable to ShippingAddress message.

msgShippingAddress = varXmlDoc;

//Assign data to each element

msgShippingAddress.Address1 = msgBillignAddress.Address1;

msgShippingAddress.Address2 = msgBillignAddress.Address2;

msgShippingAddress.City = msgBillignAddress.City;

msgShippingAddress.Country = msgBillignAddress.Country;

msgShippingAddress.State = msgBillignAddress.State;

msgShippingAddress.Zip = msgBillignAddress.Zip;

I think now you can understand how to assign one schema to another schema using Message Assignment shape in BizTalk Orchestration.

Let me know if you have any confusion in this article.

No comments:

Post a Comment