Click here to Skip to main content
15,881,204 members
Articles / Programming Languages / C#

Parsing BizTalk messages in .NET Components through Orchestration

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
2 May 2009CPOL3 min read 14.8K   7  
How to parse BizTalk messages in .NET components through orchestration.

In most of our orchestrations where we need to parse a message, let's say for storing data from the message in a database to loop through the records in the message, we use XML parsing. For that, we have the XpathNavigator class, or we can use XMLDocument's SelectNodes method which returns an iterator, and we can iterate the records in the XML. But, consider a complex XML, e.g., if there are many parent child nodes, and we have to dig deeper into the nodes, come back again to the parent, and use nested while loops for parsing the whole XML. Well, for a good coder, this won’t be much of a problem, but a messy coder like me will run into exceptions and bugs. :)

Well, how about if we take advantage of .NET Serialization/Deserialization? We pass the message as an XMLDocument object to our component method as a parameter, and we deserialize the XMLDocument to an object. But wait, which object? From where will this class come from? We just need a proxy class of the web message type. The proxy class will contain the web message type and all of its nodes, and then we can deserialize the XMLDocument object containing the XML into a much managed object which we can iterate easily. You can generate the proxy class from the Visual Studio command prompt and giving the proper switches will create the proxy class.

You can see the MSDN help for more switches of WSDL:

wsdl /out:[dir]proxy.cs http://localhost/[webservice URI].asmx?wsdl

If you notice, the Web Service URI is missing from the command, and we need a WSDL of the Web Service. A simple workaround for this is in our BizTalk Schema project in which the type of message (schema) is built and then a web message is published from the BizTalk Web Services Publishing Wizard. Just build your project and run the wizard. In the first step, select the “Publish schemas as Web Services” option. You will get a tree in the next step, and yes, the Web Service requires at least request and response schemas.

web_pub_1

Right click the request schema and select the assembly (this assembly should be your BizTalk schemas project).

choose_schema

Select the root node of your schema. In the same way, configure the response message as well (of your own choice), and you are done.

web_pub

In the next step, give the URI of the Web Service and then the hierarchy in your IIS.

By running the command given above, you will get the proxy class. Include this class in your component project (which will be most likely a .NET library project). (Note: If you don’t see your schema type class in the intellisense, do check the proxy class and your component project namespaces). In your components method which will be parsing your message, deserialize your XMLDocument object to the object type of the proxy class. This will be the Root node (if you have one) name of your schema. In my case, I used the code below. xdoc is the object variable name of the type XMLDocument which will contain my XML data. And, one more thing which I am sure most of the BizTalk guys will know is you can assign your message freely to the XMLDocument type variable in your orchestration.

C#
IFX response = new IFX();
XmlSerializer xs = new XmlSerializer(response.GetType());
XmlNodeReader reader = new XmlNodeReader(xdoc.DocumentElement,"Namespace");
response = (IFX)xs.Deserialize(reader);

In the above code, I was receiving a response and wanted to insert the records from the response into my table. This made me comfortably access the inner child records and arrays of the XML. May be, you would get XMLDocument runtime errors. Do check your XML if it contains namespaces, in which case deserialization will give exceptions. To solve this, supply the namespace of the root node in the XmlNodeReader constructor.

This article was originally posted at http://abdulrafaysbiztalk.wordpress.com?p=63

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
Qatar Qatar
I am a BizTalk Server MVP for 2010 and an active blogger and a participant on the MSDN BizTalk Server Forums. I am currently working as a BizTalk Architect at a Bank in Qatar. Before this position I worked as a Sharepoint Consultant in UAE , BizTalk Architect in Bank Saudi Fransi in Saudi Arabia and United Bank Ltd in Pakistan.

I also achieved MCTS certification in BizTalk Application development in June 2008.

Click here to check out my Blog.

SQL Server Query Notification with BizTalk Server 2009 WCF SQL Adapter.

Envelope Wrapper.

Aggregator (Sequential Convoy)

Splitter (Debatching multiple records)
Resequencer
Recipient List
Scatter and Gather using Self Correlating Port

Comments and Discussions

 
-- There are no messages in this forum --