BizTalk Server tips and tricks for developers: How to create a .NET class from a schema

Posted: February 17, 2021  |  Categories: BizTalk Schemas

Sometimes we want to bypass the adapters and perform the communication thru .NET code. Sometimes we want to access multiple values, or recursive values, of a message inside a helper class that supports an orchestration. Sometimes we want to perform a complex transformation or construct a message thru .NET code instead of a BizTalk Server map.

And for each of these circumstances, we can have several approaches, one that is simple, effective, and transversal to all of them is to use the XML Schema to generate a .NET class. This way, you have a quick and straightforward way to represent the messages you get from your BizTalk processes into a .NET class.

The easy way to generate classes that conform to a specific schema is to use the need to use the XML Schema Definition tool (Xsd.exe). You can do that by:

  • Open a Developer Command Prompt for VS <edition>
  • On the command prompt, navigate to the Schema folder and type
    • xsd /classes /language:CS Schema1.xsd

For complex schemas that import or include other schemas you need to provide all the dependencies, for example:

  • xsd /classes /language:CS Schema1.xsd Schema2.xsd

Now you will be able to import the C# class generated by the tool to your helper project and the rest is simple!

On the Orchestration you can create a variable that represents that class:

  • On property Type select <.NET Class…> and select the class that you created previously
  • From the Browse and Select .Net Type to reference, you need to select the proper assembly and the Type.

Then by using C# code inside Message Assign or Expression Shape you can serialize or deserialize XML document into C# and vice versa in a simple and straightforward way

//CONVERT MESSAGE INTO C# OBJECT
varPersonMsg = msgInput;

//CONVERT C# OBJECT INTO BIZTALK MESSAGE
msgOutput4 = varPersonMsg;

or if we have a function in a helper class like:

public static XmlDocument MapPersons(Person person)
{
  ...
}

we can directly pass the message to the function without the need to create a variable:

msgOutput4 = Support.Mapping.MapPersons(msgInput);
Author: Sandro Pereira

Sandro Pereira lives in Portugal and works as a consultant at DevScope. In the past years, he has been working on implementing Integration scenarios both on-premises and cloud for various clients, each with different scenarios from a technical point of view, size, and criticality, using Microsoft Azure, Microsoft BizTalk Server and different technologies like AS2, EDI, RosettaNet, SAP, TIBCO etc. He is a regular blogger, international speaker, and technical reviewer of several BizTalk books all focused on Integration. He is also the author of the book “BizTalk Mapping Patterns & Best Practices”. He has been awarded MVP since 2011 for his contributions to the integration community.

Leave a Reply

Your email address will not be published. Required fields are marked *

turbo360

Back to Top